首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

编译qt程序遇到奇怪的异常?求解

2012-06-13 
编译qt程序遇到奇怪的错误?求解代码如下finddialog.h#ifndef FINDDIALOG_CLASS#define FINDDIALOG_CLASS#i

编译qt程序遇到奇怪的错误?求解
代码如下 
finddialog.h 

#ifndef FINDDIALOG_CLASS
#define FINDDIALOG_CLASS
#include <QDialog>

class QLabel;
class QLineEdit;
class QPushButton;
class QCheckBox;

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 *findLabel;
QLineEdit *edit;
QPushButton *find;
QPushButton *close;
QCheckBox *box1;
QCheckBox *box2;



};
#endif


finddialog.cpp 文件

#include "finddialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
#include <QGridLayout>
#include <QtGui>
#include <QString>

FindDialog::FindDialog(QWidget *parent):QDialog(parent)
{
findLabel= new QLabel(tr("Find &What"));
//findLabel->show();
edit= new QLineEdit;
//edit->show();
findLabel->setBuddy(edit);
box1= new QCheckBox(tr("Match &case"));
box1->show();
box2= new QCheckBox(tr("Search &backward"));
box2->show();
find = new QPushButton(tr("&Find"));
find->setDefault(true);
find->setEnabled(false);
find->show();
close =new QPushButton(tr("Close"));
close->show();
connect(edit,SIGNAL(testChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));
connect(find,SIGNAL(clicked()),this,SLOT(findClicked()));
connect(close,SIGNAL(clicked()),this,SLOT(close()));

QGridLayout *layout = new QGridLayout;
layout->addWidget(findLabel,0,0);
layout->addWidget(edit,0,1);
layout->addWidget(find,0,2);
layout->addWidget(box1,1,0,1,2);
layout->addWidget(close,1,2);
layout->addWidget(box2,2,0,1,3);
//layout->show();
setLayout(layout);

}
void FindDialog::findClicked()
{
QString text = edit->text();
Qt::CaseSensitivity cs = box1->isChecked()?Qt::CaseSensitive : Qt::CaseInsensitive;
if(box2->isChecked())
{
emit findPrevious(text,cs);
}
else
{
emit findNext(text,cs);
}
}
void FindDialog::enableFindButton(const QString &text)
{
find->setEnabled(!text.isEmpty());
}
void FindDialog::findPrevious(QString const&, Qt::CaseSensitivity)
{
}
void FindDialog::findNext(QString const&, Qt::CaseSensitivity)
{
}
 
编译出现的错误如下:

finddialog.o: In function `FindDialog::FindDialog(QWidget*)':
finddialog.cpp:(.text+0xee): undefined reference to `vtable for FindDialog'
finddialog.cpp:(.text+0xf5): undefined reference to `vtable for FindDialog'
finddialog.cpp:(.text+0x10d): undefined reference to `FindDialog::staticMetaObject'
finddialog.cpp:(.text+0x1a7): undefined reference to `FindDialog::staticMetaObject'
finddialog.cpp:(.text+0x216): undefined reference to `FindDialog::staticMetaObject'
finddialog.cpp:(.text+0x285): undefined reference to `FindDialog::staticMetaObject'
finddialog.cpp:(.text+0x31a): undefined reference to `FindDialog::staticMetaObject'
finddialog.o: In function `FindDialog::FindDialog(QWidget*)':
finddialog.cpp:(.text+0x64e): undefined reference to `vtable for FindDialog'
finddialog.cpp:(.text+0x655): undefined reference to `vtable for FindDialog'
finddialog.cpp:(.text+0x66d): undefined reference to `FindDialog::staticMetaObject'
finddialog.cpp:(.text+0x707): undefined reference to `FindDialog::staticMetaObject'


finddialog.cpp:(.text+0x776): undefined reference to `FindDialog::staticMetaObject'
finddialog.cpp:(.text+0x7e5): undefined reference to `FindDialog::staticMetaObject'
finddialog.cpp:(.text+0x87a): undefined reference to `FindDialog::staticMetaObject'
collect2: ld returned 1 exit status
make: *** [finddialog] 错误 1



[解决办法]
多么熟悉的程序啊!!!这个一定就是LINUX窗口程序设计-QT精彩实例分析里面的

你直接在CPP的最前面加上,#include <QtGui>,后面的include都是浮云
[解决办法]
肯定是一开始没添加 Q_OBJECT, 后来添加该宏后,没有运行过 qmake 

题外:
#include <QtGui> 或 QtCore 是比较不好的习惯(当然,不是错误)

热点排行