我们的目的是创建一个比较丰富的Dialog。
准备3个文件, finddialog.h,finddialog.cpp,main.cpp
ffinddialog.h
2 #define FINDDIALOG_H
3 #include < QDialog >
4class QCheckBox;
5class QLabel;
6class QLineEdit;
7class QPushButton;
8class FindDialog:public QDialog
9{
10 Q_OBJECT
11public:
12 FindDialog(QWidget *parent=0);
13signals:
14void findext(const QString &str,Qt::CaseSensitivity cs);
15void findePrevious(const QString &str,Qt::CaseSensitivity cs);
16private slots:
17void findClicked();
18void enableFindButton(const QString &text);
1920private:
21 QLabel *label;
22 QLineEdit *lineEdit;
23 QCheckBox *caseCheckBox;
24 QCheckBox *backwardCheckBox;
25 QPushButton *findButton;
26 QPushButton *closeButton;
2728};
29#endif30
第10 行,Q_OBJECT 是一个宏定义,如果类里面用到了signal 或者slots,就要声明这个宏。
第12 行, FindDialog(QWidget *parent = 0);构造函数是Qt 控件类的标准格式,默认的
父参数为NULL,说明没有父控件。
第13 行,signal 声明了这个对话框发出的两个信号,如果选择向前查找,那么对话框就发出
findPrevious()信号,否则,发出findNext()信号。signal 也是一个宏,在编译之前,C++预
处理把它变成标准的c++代码。Qt::CaseSensitivity 是一个枚举类型,有Qt::CaseSensitive
和Qt::CaseInsensitive 两个值。
在类的私有部分,声明有两个slot 函数。为了实现这两个函数,需要用到对话框的其他控件的
信息,所以保存了一些控件的指针。slot 关键字和signal 一样,也是一个宏。
对于私有成员变量,我们只是使用了它们的指针,没有对它们进行存取操作,编译器不需要知道
它们的详细定义,所以只使用了这些类的前向声明。当然,也可以使用<QCheckBox>,
<QLabel>等,但是,使用前向声明会让编译速度更快一些。
finddialog.cpp
2 #include " finddialog.h "
3 FindDialog::FindDialog(QWidget * parent):QDialog(parent)
4 {
5 label = new QLabel(tr( " Find &what: " ));
6 lineEdit = new QLineEdit;
7 label -> setBuddy(lineEdit);
8 caseCheckBox = new QCheckBox(tr( " Match &case " ));
9 backwardCheckBox = new QCheckBox(tr( " Search &backward " ));
10 findButton = new QPushButton(tr( " &Find " ));
11 findButton -> setDefault( true );
12 findButton -> setEnabled( false );
13 closeButton = new QPushButton(tr( " Close " ));
14 connect(lineEdit,SIGNAL(textChanged( const QString & )), this ,SLOT(enableFindButton( const QString & )));
15 connect(findButton,SIGNAL(clicked()), this ,SLOT(findClicked()));
16 connect(closeButton,SIGNAL(clicked()), this ,SLOT(close()));
17 QHBoxLayout * topLeftLayout = new QHBoxLayout();
18 topLeftLayout -> addWidget(label);
19 topLeftLayout -> addWidget(lineEdit);
20 QVBoxLayout * leftLayout = new QVBoxLayout();
21 leftLayout -> addLayout(topLeftLayout);
22 leftLayout -> addWidget(caseCheckBox);
23 leftLayout -> addWidget(backwardCheckBox);
24 QVBoxLayout * rightLayout = new QVBoxLayout();
25 rightLayout -> addWidget(findButton);
26 rightLayout -> addWidget(closeButton);
27 rightLayout -> addStretch();
28 QHBoxLayout * mainLayout = new QHBoxLayout();
29 mainLayout -> addLayout(leftLayout);
30 mainLayout -> addLayout(rightLayout);
31 setLayout(mainLayout);
32 setWindowTitle(tr( " Find " ));
33 setFixedHeight(sizeHint().height());
34
35
36 }
37
38 void FindDialog::findClicked()
39 {
40 QString text = lineEdit -> text();
41 Qt::CaseSensitivity cs = caseCheckBox -> isChecked() ? Qt::CaseSensitive:Qt::CaseInsensitive;
42 if (backwardCheckBox -> isChecked()){
43 emit findePrevious(text,cs);
44
45 } else {
46 emit findext(text,cs);
47 }
48 }
49
50 void FindDialog::enableFindButton( const QString & text)
51 {
52 findButton -> setEnabled( ! text.isEmpty());
53
54 }
55
当用户点击findButton 按钮,findClicked()就会调用,根据backwardCheckBox 状态,他
发出findPrevious()或者findNext()信号。emit 也是一个Qt 的宏。
当用户改变lineEdit 中的文本,enableFindButton()slot 函数就会调用。如果输入了文本,那
么让findButton 有效,否则就无效。
当父控件没销毁qt会自动销毁所有子控件
main.cpp
2 FindDialog * dialog = new FindDialog();
3 dialog -> show();
4 return app.exec();
。Qt 有三个主要的布局管理器:
QHBoxLayout:水平排列控件。
QVBoxLayout:垂直排列控件。
QGridLayout:按矩阵方式排列控件