我在使用qt 4第二版本书在visual c express 2010上编译一个qt示例时遇到问题 . 自从qt visual studio插件没有't work with express edition , I configured it by myself by just adding library dependencies: qtmaind.lib QtCored4.lib QtGuid4.lib .Also I can compile a sample code '你好,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:未解析的外部符号“protected:void __thiscall findDialog :: findNext(class QString const&,enum Qt :: CaseSensitivity)”(?findNext @ findDialog @@ IAEXABVQString @@ W4CaseSensitivity @ Qt @@@ Z)在函数“private”中引用:void __thiscall findDialog :: findClicked(void)“(?findClicked @ findDialog @@ AAEXXZ)
LNK2019:未解析的外部符号“protected:void __thiscall findDialog :: findPrevious(class QString const&,enum Qt :: CaseSensitivity)”(?findPrevious @ findDialog @@ IAEXABVQString @@ W4CaseSensitivity @ Qt @@@ Z)在函数“private”中引用:void __thiscall findDialog :: findClicked(void)“(?findClicked @ findDialog @@ AAEXXZ)
提前谢谢你,抱歉我的英语不好 .
4 回答
也许你可以根据Kia.celever的诊断和更多信息解决你的问题
a claim of success
one source (1) is based on
fairly typical posting in QT forum (summary: expect problems)
但是所有这些工作都不会进一步提高你的编程知识/技能,c或qt . 如果Visual Studio对您很重要,请投资使用不受限制的版本以供专业人员使用,并安装插件,避免配置工具以使其无法使用的麻烦 . 否则,安装QT-Creator(参见(2); Qt Creator IDE and tools) .
链接错误来自
Qt's Meta Object Compiler
.只需在类中编写
Q_OBJECT
宏时,它就会在类中定义一些Qt处理事件所需的成员函数....Qt MOC将创建一个新的.cpp文件并在其中编写这些函数的主体 .
链接错误清楚地表明缺少FindDilog(名为
moc_FindDialog.cpp
)的特定.cpp文件 . 这意味着Qt不会生成moc文件 .也许Visual Studio忘记调用Qt Moc来生成.cpp文件 . 也许Visual Studio编译器没有编译它...
由于你没有Qt集成,我检查了我的一个Qt项目的设置 .
在
FindDialog.h
的property
页面上,将Item Type
从C/C++ header
更改为Custom Build Tool
. 按Apply
时,左侧会出现一个新的Custom Build Tool
项目 . 展开它,在General
中你会找到Command Line
,输入这是我发布版本的命令行 . 您应该根据您的配置进行更改 . 请记住将生成的cpp添加到项目中 .
在
Custom Build Tool
下,有一个名为Outputs
的项目,应该是由于我可能仍然缺少某些内容,以下是vc项目文件中此文件的部分:
请注意,这是来自VS2008的
vcproj
.确保在.pro文件中明确包含findDialog.h . 如果不是,请添加它并确保在尝试构建项目之前再次运行qmake .