1、源代碼
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>//它是對話框的基類,從QWidget類中派生出來
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
//接下來是四個類的前置聲明
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
//接下來定義FindDialog,并讓它成為QDialog的子類
class FindDialog: public QDialog
{
Q_OBJECT
//對于所有定義了信号和槽的類,在類定義開始處的Q_OBJECT宏是必須的。
public:
FindDialog(QWidget * parent = 0);
//parent參數指定了它的父視窗部件,該參數為空,意味着該對話框沒有父對象
signals:
//當使用者單擊Find按鈕時對話框會發射下面定義的兩個信号,signals也是一個宏
void findNext(const QString & str, Qt::CaseSensitivity cs);
void findPrevious(const QString & str, Qt::CaseSensitivity cs);
//Qt::CaseSensitivity是一個枚舉類型,它有Qt::CaseSensitive和Qt::CaseInsensitive兩個取值
private slots://slots像signal一樣也是一個宏
void findClicked();
void enableFindButton(const QString & text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif // FINDDIALOG_H
#include "finddialog.h"
#include <QtGui>
#include <QHBoxLayout>
FindDialog::FindDialog(QWidget *parent):QDialog(parent)
//把parent參數傳遞給了基類的構造函數,然後建立子視窗部件
{
label = new QLabel(tr("Find &what:"));//建立了一個帶有快捷鍵(Alt+W)的标簽
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);//設定了行編輯器作為标簽的夥伴。夥伴就是一個視窗部件。
caseCheckBox = new QCheckBox(tr("Math &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));//建立了一個Find按鈕,使用者
findButton->setDefault(true);//讓Find按鈕稱為對話框的預設按鈕
findButton->setEnabled(false);//禁用了Find按鈕,當禁用一個部件時,它會顯示為灰色
/*
findButton = new QPushButton(tr("&Find"));//建立了一個Find按鈕
使用者可在那些支援快捷鍵的平台下通過按下Alt+F來激活它。符号&可以用來控制焦點。
*/
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()));
/*
1、當行編輯器中的文本發生了變化,就會調用私有槽enableFindButton(const QString &)
2、當使用者單擊Find按鈕時,會調用findclicked()私有槽。
3、當使用者單擊close時,對話框會關閉。
因為QObject是FindDialog的父對象之一,是以省略了connec()函數前面的QObject::字首
*/
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);//主布局裝到對話框中
/*
setLayout(mainLayout);執行完這一句,也就是主布局裝到對話框中去時,它會成為
對話框的子對象,于是所有的子視窗部件都會重定義自己的父對象,進而變成對話框中的子對象。
leftLayout->addLayout(topLeftLayout);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
當将子布局對象添加到父布局對象中時,子布局對象就會自動重定義自己的父對象。
*/
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
/*
設定了顯示在對話框标題欄上的标題的内容,并讓視窗具有一個固定的高度,這是因為在
對話框的垂直方向上再沒有其他視窗部件可以去占用所多出的空間了。
QWidget::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());
}
#include <QApplication>
#include "finddialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return a.exec();
}

2、運作結果:
Qt——查找對話框執行個體
3、深入介紹信号和槽
槽和信号連接配接在一起,每當發射一個信号的時候,就會自動調用這個槽。連接配接語句如下:
connect(sender, SIGNAL(signal),receiver,SLOT(slot));
其中,sender和receiver是對象的指針,signal和slot是不帶參數的函數。
- 一個信号可以連接配接多個槽
connect(slider, SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));
connect(slider, SIGNAL(valueChanged(int)),this,SLOT(updateStatusBar(int)));
- 多個信号連接配接同一個槽
connect(lcd, SIGNAL(overflow()),this,SLOT(handleMathError()));
connect(calculator, SIGNAL(divisionByZero()),this,SLOT(handleMathError()));
- 一個信号與另一個信号連接配接
connect(lineEdit, SIGNAL(textChanged(const QString &)),this,SLOT(updateRecord(const QString &)));
- 連接配接可以被拆除
disconnect(lcd, SIGNAL(overflow()),this,SLOT(handleMathError()));
這個情況用的比較少,因為當删除對象時,Qt會自動移除和這個對象相關的所有連接配接。