Find Dialog
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类的构造函数,parent 参数指定了它的父窗口部件,默认值是空指针, 则没有父对象*/ signals: void findNext(const QString &str, Qt::CaseSensitivity cs); void findPrevious(const QString &str, Qt::CaseSensitivity cs);/*signals部分声明了当用户单击Find按钮时对话框所发射的两个信号 Qt::sensitivity是一个枚举类型,他有Qt::CaseInsensitive 和 Qt::CaseSensitive两个取值*/ 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:
1 #include <QtGui> 2 3 #include "FindDialog.h" 4 5 FindDialog::FindDialog(QWidget *parent)/*FindDialog 的构造函数*/ 6 : QDialog(parent) 7 { 8 label = new QLabel(tr("Find &what:")); 9 lineEdit = new QLineEdit; 10 label->setBuddy(lineEdit); 11 12 caseCheckBox = new QCheckBox(tr("Match &case")); 13 backwardCheckBox = new QCheckBox(tr("Search &backward")); 14 15 findButton = new QPushButton(tr("&Find")); 16 findButton->setDefault(true); 17 findButton->setEnabled(false); 18 19 closeButton = new QPushButton(tr("Close")); 20 21 connect(lineEdit,SIGNAL(textChanged(const QString& )), 22 this,SLOT(enableFindButton(const QString& ))); 23 connect(findButton,SIGNAL(clicked()), 24 this, SLOT(findClicked())); 25 connect(closeButton,SIGNAL(clicked()), 26 this, SLOT(close())); 27 28 QHBoxLayout *topLeftLayout = new QHBoxLayout; 29 topLeftLayout->addWidget(label); 30 topLeftLayout->addWidget(lineEdit); 31 32 QVBoxLayout *leftLayout = new QVBoxLayout; 33 leftLayout->addLayout(topLeftLayout); 34 leftLayout->addWidget(caseCheckBox); 35 leftLayout->addWidget(backwardCheckBox); 36 37 QVBoxLayout *rightLayout = new QVBoxLayout; 38 rightLayout->addWidget(findButton); 39 rightLayout->addWidget(closeButton); 40 rightLayout->addStretch(); 41 42 QHBoxLayout *mainLayout = new QHBoxLayout; 43 mainLayout->addLayout(leftLayout); 44 mainLayout->addLayout(rightLayout); 45 setLayout(mainLayout); 46 47 setWindowTitle(tr("Find")); 48 setFixedHeight(sizeHint().height()); 49 50 } 51 52 void FindDialog::findClicked() 53 { 54 QString text = lineEdit->text(); 55 Qt::CaseSensitivity cs = 56 caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; 57 if(backwardCheckBox->isChecked()){ 58 emit findPrevious(text,cs); 59 } 60 else { 61 emit findNext(text,cs); 62 } 63 64 } 65 66 void FindDialog::enableFindButton(const QString &text) 67 { 68 findButton->setEnabled(!text.isEmpty()); 69 }