现在的位置: 首页 > 综合 > 正文

subclass QDialog

2013年10月10日 ⁄ 综合 ⁄ 共 3713字 ⁄ 字号 评论关闭
 
finddialog.h
  1. #ifndef FINDDIALOG_H
  2. #define FINDDIALOG_H
  3. #include <QDialog>
  4. class QCheckBox;
  5. class QLabel;
  6. class QLineEdit;
  7. class QPushbutton;
  8. class FindDialog : public QDialog
  9. {
  10.     Q_OBJECT
  11. public:
  12.     FindDialog(QWidget *parent=0);
  13.     ~FindDialog();
  14.   
  15. signals:
  16.     void findNext(const QString &str,Qt::CaseSensitivity cs);
  17.     void findPrevious(const QString &str, Qt::CaseSensitivity cs);
  18.     private slots:
  19.         void findClicked();
  20.         void enableFindButton(const QString &text);
  21. private:
  22.     QLabel *label;
  23.     QLineEdit *lineEdit;
  24.     QCheckBox *caseCheckBox;
  25.     QCheckBox *backwardCheckBox;
  26.     QPushButton *findButton;
  27.     QPushButton *closeButton;
  28.     
  29. };
  30. #endif // FINDDIALOG_H

finddialog.cpp

  1. #include<QtGui>
  2. #include "finddialog.h"
  3. FindDialog::FindDialog(QWidget *parent)
  4.     : QDialog(parent)
  5. {
  6.     label= new QLabel(tr("Find &what:"));
  7.     lineEdit=new QLineEdit;
  8.     label->setBuddy(lineEdit);
  9.     caseCheckBox=new QCheckBox(tr("Match &case"));
  10.     backwardCheckBox=new QCheckBox(tr("Search &backward"));
  11.     findButton=new QPushButton(tr("&Find"));
  12.     findButton->setDefault(true);
  13.     findButton->setEnabled(false);
  14.     closeButton=new QPushButton(tr("Close"));
  15. connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));
  16. connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
  17. connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
  18. QHBoxLayout *topleftLayout=new QHBoxLayout;
  19. topleftLayout->addWidget(label);
  20. topleftLayout->addWidget(lineEdit);
  21. QVBoxLayout *leftLayout=new QVBoxLayout;
  22. leftLayout->addLayout(topleftLayout);
  23. leftLayout->addWidget(caseCheckBox);
  24. leftLayout->addWidget(backwardCheckBox);
  25. QVBoxLayout *rightLayout=new QVBoxLayout;
  26. rightLayout->addWidget(findButton);
  27. rightLayout->addWidget(closeButton);
  28. rightLayout->addStretch();
  29. QHBoxLayout *mainLayout=new QHBoxLayout;
  30. mainLayout->addLayout(leftLayout);
  31. mainLayout->addLayout(rightLayout);
  32. setLayout(mainLayout);
  33. setWindowTitle(tr("Find"));
  34. setFixedHeight(sizeHint().height());
  35. }
  36. void FindDialog::findClicked()
  37. {
  38.     QString text=lineEdit->text();
  39.     Qt::CaseSensitivity cs=caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
  40.   
  41.     if(backwardCheckBox->isChecked()){
  42.         emit findPrevious(text,cs);
  43.     }else{
  44.         emit findNext(text,cs);
  45.     }
  46. }
  47. void FindDialog::enableFindButton(const QString &text)
  48. {
  49.     findButton->setEnabled(!text.isEmpty());
  50. }
  51. FindDialog::~FindDialog()
  52. {
  53. }
  54. //  出现 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) 中被引用
  55. //   1>D:/grid/C++/QT/Debug/QT.exe : fatal error LNK1120: 1 个无法解析的外部命令
  56. //  可能是有一个函数只在头文件中声明了, 但没有在。cpp中去定义实现它, 所以导致链接时, 当主文件中调用此函数但又不可能找到(因为其他编译文件中没有定义)就会出现这种错误
  57. //
  58. //   本文调试过程中 便出现这个问题 ,后来经查明,是void FindDialog::enableFindButton(const QString &text) 没有在.cpp中定义 

main.cpp

  1. #include <QtGui/QApplication>
  2. #include "finddialog.h"
  3.  int main(int argc, char *argv[])
  4.  {
  5.      QApplication app(argc, argv);
  6.      FindDialog *dialog=new FindDialog;
  7.      dialog->show();
  8.      return app.exec();
  9.  }
  10.     

在一开始调试过程中出现以下错误
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中定义

抱歉!评论已关闭.