QT 初阶 第二章 创建对话框(查找对话框实例)

  最终效果图: 

该对话框由三个文件组成:finddialog.h 、finddialog.cpp、 main.cpp

代码+注释

 1 /*--finddialog.h--*/
 2 #ifndef FINDDIALOG_H
 3 #define FINDDIALOG_H
 4 #include <QDialog>
 5 /*Qt中类的前向声明,会让编译速度更快*/
 6 class QCheckBox;
 7 class QLabel;
 8 class QLineEdit;
 9 class QPushButton;
10 class FindDialog:public QDialog//FindDialog 从QDialog继承
11 {
12     Q_OBJECT//宏定义,如果类中使用了signal或者slots就要声明这个宏
13 public:
14     FindDialog(QWidget *parent = 0);//构造函数是Qt控件类的标准格式,默认的父参数为NULL ,说明没有父控件
15 signals:/*声明了这个对象发出两个信号,向前查找和向后查找信号.signal也是一个宏,编译之前,C++预处理把它变成标准的C++代码*/
16     void findNext(const QString &str,Qt::CaseSensitivity cs);//Qt::CaseSensitivity是一个枚举类型,有两个值Qt::CaseSensitive,Qt::CaseInsensitive
17     void findPrevious(const QString &str,Qt::CaseSensitivity cs);
18 private slots:/*slot和signal一样也是个宏。声明了两个slot函数,为了实现函数需要用到对话框的其他控件的信息,所以保存了一些控件的指针*/
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 #endif // FINDDIALOG_H
30 /*更多内容待更新,AQ 2016.10.25 信息楼555室*/

 

 1 /*--finddialog.cpp--*/
 2 #include <QtGui>
 3 #include <QtWidgets>
 4 #include <QLabel>
 5 #include <QLineEdit>
 6 #include "finddialog.h"
 7 FindDialog::FindDialog(QWidget *parent):QDialog(parent)
 8 {
 9     label = new QLabel(tr("Find &what:"));//创建一个label标签,并初始化标签
10     lineEdit = new QLineEdit;//创建一个文本框对象
11     label->setBuddy(lineEdit);//label 的快捷键是 "ALT+w",按下快捷键时,输入焦点自动跳到label的buddy——lineEdit上
12     
13     caseCheckBox = new QCheckBox(tr("Match &case"));
14     backwardCheckBox = new QCheckBox(tr("Search &backward"));
15     /*初始化了两个复选框,并且初始化复选框的标签文字*/
16     
17     findButton = new QPushButton(tr("&Find"));
18     findButton->setDefault(true);
19     findButton->setEnabled(false);
20     /*创建Find按钮,设置该按钮为对话框默认按钮,即用户按下回车,由该按钮接受事件,并设置按钮当前不可用*/
21     
22     closeButton=new QPushButton(tr("Close"));//创建close按钮
23     
24     connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));
25     connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
26     connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
27     /*以上绑定(连接)信号,第一个文本框内容发生改变,则调用自己(this)的enableFindButton(...)响应函数
28       第二个用户按下find按钮发出clicked()事件,由它自己(this)的findClicked()函数去执行
29       第三个用户点击close按钮发出clicked()事件,由它自己(this)的close()函数去执行
30     */
31     
32     QHBoxLayout *topLeftLayout = new QHBoxLayout;//创建水平排列控件
33     topLeftLayout->addWidget(label);
34     topLeftLayout->addWidget(lineEdit);
35     /*添加标签和文本框到topLeftLayout布局中*/
36     
37     QVBoxLayout *leftLayout = new QVBoxLayout;//创建垂直排列控件
38     leftLayout->addLayout(topLeftLayout);
39     leftLayout->addWidget(caseCheckBox);
40     leftLayout->addWidget(backwardCheckBox);
41     /*添加topLeftLayout水平控件和两个复选框到leftLayout水平控件中*/
42     
43     QVBoxLayout *rightLayout = new QVBoxLayout;//创建垂直排列控件
44     rightLayout->addWidget(findButton);
45     rightLayout->addWidget(closeButton);
46     rightLayout->addStretch();//**平均分配
47     /*添加两个按钮find和close到rightLayout中,并平均分配其位置空间*/
48     
49     QHBoxLayout *mainLayout= new QHBoxLayout;
50     mainLayout->addLayout(leftLayout);
51     mainLayout->addLayout(rightLayout);
52     /*创建水平排列控件,并将leftLayout和rightLayout两个布局控件添加到mainLayout布局中*/
53     
54     setLayout(mainLayout);//设置主窗体的布局为mainLayout
55     
56     setWindowTitle(tr("Find"));//设置主窗体的标题
57     setFixedHeight(sizeHint().height());//设置窗体的高度
58 }
59 void FindDialog::findClicked()//实现响应函数
60 {
61     QString text = lineEdit->text();//取得文本框中的内容
62     
63     Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
64     if(backwardCheckBox->isChecked()){
65         emit findPrevious(text,cs);//emit也是个宏
66     }else{
67         emit findNext(text,cs);
68     }
69     /*根据backwardCheckBox状态发出findPrevious或者findNext信号执行*/
70 }
71 void FindDialog::enableFindButton(const QString &text){//响应函数,如果用户改变文本框内容,调用该函数
72     findButton->setEnabled(!text.isEmpty());//设置find()按钮可用
73 }
74 /*更多内容待更新,AQ 2016.10.25 信息楼555室*/

 

 1 /*--main.cpp--*/
 2 #include "mainwindow.h"
 3 #include <QApplication>
 4 #include "finddialog.h"
 5 
 6 int main(int argc, char *argv[])
 7 {
 8     QApplication a(argc, argv);//入口
 9     FindDialog *dialog = new FindDialog;
10     dialog->show();
11     return a.exec();
12 }
13 /*更多内容待更新,AQ 2016.10.25 信息楼555室*/

 

posted @ 2016-10-25 19:30  马丁黄瓜啊  阅读(1499)  评论(0编辑  收藏  举报