main.cpp
#include <QApplication> #include "finddialog.h"
int main(int argc, char *argv[]) { QApplication app(argc, argv); FindDialog *dialog = new FindDialog; dialog->show(); return app.exec(); }
finddialog.h
#ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QDialog>
//前置聲明一些用到的Qt類,會告訴編譯器類的存在,而不用提供類定義中的所有細節,這可以使編譯過程更快一些
class QCheckBox; class QLabel; class QLineEdit; class QPushButton; class FindDialog : public QDialog { Q_OBJECT //對於所有定義了信號與槽的類,Q_OBJECT宏都是必須的 public: FindDialog(QWidget *parent = 0); //parent參數指定了父窗口部件,默認值是一個空指針,意味着沒有父對象 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> #include "finddialog.h" FindDialog::FindDialog(QWidget *parent) : QDialog(parent) //把parent傳遞給基類的構造函數 { label = new QLabel(tr("Find &what:")); //tr()函數把它們翻譯成其他語言的標記,在每個QObject對象以及包含有Q_OBJECT宏的子類中都有其聲明 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); //設置為默認按鈕,即用戶按下Enter鍵時能夠按下對應的按鈕 findButton->setEnabled(false); //禁用 closeButton = new QPushButton(tr("Close"));
//由於QObject是FindDialog的父對象之一,所以可以省略connect()函數前面的QObject::前綴 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()); //sizeHint()返回一個窗口部件的理想的尺寸大小 } 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()); }
運行效果:
附加小知識:
1:
創建窗口部件和布局時使用的是new,理應寫一個delete的析構函數,但在qt中,在刪除父對象的時候會自動刪除其所屬的所有子對象
2:
默認的Tab鍵順序就是創建窗口部件時的順序,要改變這個順序,可以使用QWidget::setTabOrder()函數