我该如何解决这个错误?

时间:2016-04-01 11:49:37

标签: c++ qt

当我在阅读本书时尝试运行此示例代码时遇到此错误 - “使用Qt进行C ++ GUI编程”。

gotocelldialog.h:

    #ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

#include <QDialog>
#include "ui_gotocelldialog.h"
class gotocelldialog : public QDialog, public Ui::goToCellDialog
{
    Q_OBJECT
 public:
 explicit gotocelldialog(QWidget *parent = 0);

 signals:

 public slots:
 void on_lineEdit_textChanged();
};

 #endif // GOTOCELLDIALOG_H

gotocelldialog.cpp:

#include "gotocelldialog.h"
#include <QtGui>
gotocelldialog::gotocelldialog(QWidget *parent) :
    QDialog(parent)
 {
    setupUi(this); 
    QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
    lineEdit->setValidator(new QRegExpValidator(regExp,this));
    connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(cancelButton,SIGNAL(clicked()),this,SLOT(accept()));
 }

void gotocelldialog::on_lineEdit_textChanged(){
    okButton->setEnabled(lineEdit->hasAcceptableInput());
}

main.cpp中:

#include <QApplication>
   #include <QDialog>
   #include "gotocelldialog.h"

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);

       gotocelldialog* dialog = new gotocelldialog();
      dialog->show();
        return a.exec();
    }

运行此程序时,会显示以下错误消息:

/home/ayush/untitled3/gotocelldialog.cpp:6: error: no matching function for call to 'gotocelldialog::setupUi(gotocelldialog*)'
     setupUi(this);
                 ^
../untitled3/gotocelldialog.cpp:6:17: note: candidate is:
In file included from ../untitled3/gotocelldialog.h:5:0,
                 from ../untitled3/gotocelldialog.cpp:1:
./ui_gotocelldialog.h:49:10: note: void Ui_goToCellDialog::setupUi(QMainWindow*)
     void setupUi(QMainWindow *goToCellDialog)
          ^
./ui_gotocelldialog.h:49:10: note:   no known conversion for argument 1 from 'gotocelldialog*' to 'QMainWindow*'
Makefile:344: recipe for target 'gotocelldialog.o' failed
make: *** [gotocelldialog.o] Error 1
17:15:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project untitled3 (kit: Desktop)
When executing step 'Make'

出现此错误的原因是什么?如何解决?

1 个答案:

答案 0 :(得分:1)

您的ui文件看起来定义了QMainWindow,但您的代码需要QDialog

因此存在不匹配问题,您应该从QDialog模板重新创建ui文件,或者在编辑器中更改它。