finddialog.h
- #ifndef FINDDIALOG_H
- #define FINDDIALOG_H
- #include <QDialog>
- class QCheckBox;
- class QLabel;
- class QLineEdit;
- class QPushbutton;
- class FindDialog : public QDialog
- {
- Q_OBJECT
- public:
- FindDialog(QWidget *parent=0);
- ~FindDialog();
-
- 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_H
finddialog.cpp
- #include<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());
- }
- FindDialog::~FindDialog()
- {
- }
main.cpp
- #include <QtGui/QApplication>
- #include "finddialog.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- FindDialog *dialog=new FindDialog;
- dialog->show();
- return app.exec();
- }
-
在一开始调试过程中出现以下错误
error LNK2019: 无法解析的外部符号 "private: void __thiscall FindDialog::enableFindButton(class QString const &)" (?enableFindButton@FindDialog@@AAEXABVQString@@@Z),该符号在函数 "public: virtual int __thiscall FindDialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@FindDialog@@UAEHW4Call@QMetaObject@@HPAPAX@Z) 中被引用
1>D:/grid/C++/QT/Debug/QT.exe : fatal error LNK1120: 1 个无法解析的外部命令
原因: 可能是有一个函数只在头文件中声明了, 但没有在。cpp中去定义实现它, 所以导致链接时, 当主文件中调用此函数但又不可能找到(因为其他编译文件中没有定义)就会出现这种错误
本文调试过程中 便出现这个问题 ,后来经查明,是void FindDialog::enableFindButton(const QString &text) 没有在.cpp中定义