【发布时间】:2016-07-21 05:13:13
【问题描述】:
我在阅读《C++ GUI Programming with Qt》一书时尝试运行此示例代码时遇到了这个错误。
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'
这个错误的原因是什么?如何解决?
【问题讨论】:
-
您希望
setupUi(this);调用什么函数?编译器希望它是gotocelldialog的成员函数,但gotocelldialog没有这样的函数。 -
代码看起来很合理,除非
Ui::goToCellDialog被破坏。这是整个错误消息吗? -
setupUi()应该是由 UIC 生成的Ui::goToCellDialog的一部分。您可能想要打开ui_gotocelldialog.h并确保它已被实际定义。如果没有,那么问题来了:为什么 UIC 没有为它生成代码?