解决Qt编写对话框出现的问题

来源:互联网 发布:驾驶远程教育及时软件 编辑:程序博客网 时间:2024/06/05 12:46

1.   VS2008中,Qt Designer设计完对话框 xxx.ui ,用 uic.exe 生成头文件,用法: uic xxx.ui>ui_xxx.h

2.   将生成的 ui_xxx.h 添加进工程,在工程中添加类 Cxxx ,

xxx.h 中从 QDialog 和 ui_xxx.h 中继承过来:

class CGoToCellDialog : public QDialog, public Ui::GoToCellDialog{Q_OBJECTpublic:CGoToCellDialog(QWidget* parent = 0);~CGoToCellDialog(void);private slots:void on_lineEdit_textChanged();};

xxx.cpp 中在构造函数中这样写:


CGoToCellDialog::CGoToCellDialog(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(reject()));}CGoToCellDialog::~CGoToCellDialog(void){}void CGoToCellDialog::on_lineEdit_textChanged(){okButton->setEnabled(lineEdit->hasAcceptableInput());}

3.   其中, setupUi 中会将 QtDesigner 中画的界面显示出来, setupUi 还会自动创建信号槽,规则为要符合 on_objectName_signalName() 的命名管理的任意槽于相应的

objectName 的 signalName的信号连接到一起,在这里有一个叫 lineEdit 的文本框,所有会自动完成下面的代码

connect( lineEdit, SIGNAL(textChanged()), this, SLOT(on_lineEdit_textChanged())) 。


4.   至此,编译程序,出现如下错误:

错误 1 error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall Widget::metaObject(void)const " (?metaObject@Widget@@UBEPBUQMetaObject@@XZ) 
错误 2 error LNK2001: 无法解析的外部符号 "public: virtual void * __thiscall Widget::qt_metacast(char const *)" (?qt_metacast@Widget@@UAEPAXPBD@Z) 
错误 3 error LNK2001: 无法解析的外部符号 "public: virtual int __thiscall Widget::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Widget@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
错误 4 fatal error LNK1120: 3 个无法解析的外部命令


这是因为在源文件中没有添加上 moc_xxx.cpp 文件,解决办法:

命令行:moc.exe xxx.h -o moc_xxx.cpp ,输出 moc_xxx.cpp ;将之添加至源文件中,再次编译,生成成功!


0 0