【发布时间】:2012-02-01 15:40:31
【问题描述】:
我在编译 c++ GUI programming with qt 4 second edition book on visual c++ express 2010 中的 qt 示例时遇到问题。由于 qt Visual Studio 插件不适用于 express edition ,我自己通过添加库依赖项来配置它: qtmaind.lib QtCored4.lib QtGuid4.lib 。我还可以编译示例代码“你好,Qt!”没有错误。
我的项目包含两个 .cpp 文件和一个头文件:
findDialog.h:
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QtGui\qdialog.h>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class findDialog : public QDialog
{
Q_OBJECT
public:
findDialog(QWidget* parent = 0);
signals:
void findNext(const QString &str , Qt::CaseSensitivity cs);
void findPrevious(const QString &str , Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindButton(const QString& text);
private:
QLabel* label;
QLineEdit* lineEdit;
QCheckBox* caseCheckBox;
QCheckBox* backwardCheckBox;
QPushButton* findButton;
QPushButton* closeButton;
};
#endif
findDialog.cpp:
#include <QtGui\QtGui>
#include "findDialog.h"
findDialog::findDialog(QWidget* parent) : QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
connect(lineEdit , SIGNAL(textChanged(const QString&)) , this , SLOT(enableFindButton(const QString&)));
connect(findButton , SIGNAL(clicked()) , this , SLOT(findClicked()));
connect(closeButton , SIGNAL(clicked()) , this , SLOT(close()));
QHBoxLayout* topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}
void findDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
emit findPrevious(text , cs);
else
emit findNext(text , cs);
}
void findDialog::enableFindButton(const QString& text)
{
findButton->setEnabled(!text.isEmpty());
}
main.cpp:
#include <QtGui\qapplication.h>
#include <iostream>
#include "findDialog.h"
int main(int argc , char* argv[])
{
QApplication app(argc , argv);
findDialog* dialog = new findDialog;
dialog->show();
return app.exec();
}
当我编译这个项目时,我得到 6 个链接错误:
LNK2001: 未解析的外部符号“public: virtual struct QMetaObject const * __thiscall findDialog::metaObject(void)const” (?metaObject@findDialog@@UBEPBUQMetaObject@@XZ)
LNK2001:未解析的外部符号“public: virtual void * __thiscall findDialog::qt_metacast(char const *)”(?qt_metacast@findDialog@@UAEPAXPBD@Z)
LNK2001: 未解析的外部符号“public: virtual int __thiscall findDialog::qt_metacall(enum QMetaObject::Call,int,void * *)” (?qt_metacall@findDialog@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
LNK2001:未解析的外部符号“public: static struct QMetaObject const findDialog::staticMetaObject”(?staticMetaObject@findDialog@@2UQMetaObject@@B)
LNK2019:在函数“私有:void __thiscall findDialog::findClicked(void)”(?findClicked@findDialog@@AAEXXZ)
LNK2019:在函数“私有:void __thiscall findDialog::findClicked(void)”(?findClicked@findDialog@@AAEXXZ)
提前谢谢你,对不起我的英语不好。
【问题讨论】:
-
你生成 moc_findDialog.cpp 了吗?
标签: c++ visual-studio-2010 qt