以一个查找对话框为例子开始学习 用Qt创建一个对话框程序

首先使用手工编码创建一个查找对话框程序

 1 #include "finddialog.h"
 2 #include<QtGui>
 3 
 4 FindDialog::FindDialog(QWidget *parent) :
 5     QDialog(parent)
 6 {  //查找窗口的部件的创建和初始化
 7     text    = new QLineEdit;
 8     caseSensCheck    = new QCheckBox(tr("Match Case"));
 9     prevCheck    = new QCheckBox(tr("Backward"));
10     findText    = new QPushButton(tr("Find"));
11     stopFind    = new QPushButton(tr("Stop"));
12     //信号-槽的链接
13     this->connect(text,SIGNAL(textChanged(const QString&)),
14               this,SLOT(findBtnEnabled(const QString&)));
15     this->connect(stopFind,SIGNAL(clicked()),
16               this,SLOT(close()));
17     this->connect(findText,SIGNAL(clicked()),
18               this,SLOT(findClicked()));
19     this->connect(this,
20               SIGNAL(findPrevious(const QString&,Qt::CaseSensitivity)),
21               this,
22               SLOT(findPrevious_Slot(const QString&,Qt::CaseSensitivity)));
23     this->connect(this,
24               SIGNAL(findNext(const QString&,Qt::CaseSensitivity)),
25               this,
26               SLOT(findNext_Slot(const QString&,Qt::CaseSensitivity)));
27     //窗口部件的状态
28     caseSensCheck->setChecked(false);
29     prevCheck->setChecked(false);
30     findText->setEnabled(false);
31     stopFind->setEnabled(true);
32     this->setWindowTitle(tr("FindDialog"));
33 
34     //窗口布局的初始化
35     hLayout = new QHBoxLayout;
36     vLeftLayout = new QVBoxLayout;
37     vRightLayout = new QVBoxLayout;
38     vLeftLayout->addWidget(text);
39     vLeftLayout->addWidget(caseSensCheck);
40     vLeftLayout->addWidget(prevCheck);
41     vRightLayout->addWidget(findText);
42     vRightLayout->addWidget(stopFind);
43     hLayout->addLayout(vLeftLayout);
44     hLayout->addLayout(vRightLayout);
45 
46     this->setLayout(hLayout);
47 }
48 
49 //向前查找槽函数
50 void    FindDialog::findPrevious_Slot(const QString&txt,Qt::CaseSensitivity cs)
51 {
52     this->setWindowTitle(tr("FindPrevious"));
53     this->update();
54 }
55 
56 //向后查找槽函数
57 void    FindDialog::findNext_Slot(const QString&txt,Qt::CaseSensitivity cs)
58 {
59     this->setWindowTitle(tr("FindNext"));
60     this->update();
61 }
62 
63 //查找按钮点击槽函数
64 void    FindDialog::findClicked()
65 {
66     QString    txt = text->text();
67   //获取是否大小写匹配的状态
68     Qt::CaseSensitivity    cs =
69         caseSensCheck->isChecked()?Qt::CaseSensitive:
70                        Qt::CaseInsensitive;
71     if(prevCheck->isChecked())
72         emit    findPrevious(txt,cs);//发射向前查找信号
73     else
74         emit    findNext(txt,cs);//发射向后查找信号
75 }
76 //停止查找槽函数
77 void    FindDialog::stopClicked()
78 {
79     this->close();
80 }
81 //查找按钮使能槽函数
82 void    FindDialog::findBtnEnabled(const QString &txt)
83 {
84     findText->setEnabled(!txt.isEmpty());
85 }
86 //析构函数,销毁窗口部件 和 窗口布局 
87 FindDialog::~FindDialog()
88 {
89     delete    text;
90     delete    findText;
91     delete    stopFind;
92     delete    caseSensCheck;
93     delete    prevCheck;
94     delete    vLeftLayout;
95     delete    vRightLayout;
96     delete    hLayout;
97 }

 

 //main.cpp
1
#include <QApplication> 2 #include<finddialog.h> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv);
    //创建并显示查找窗口
7 FindDialog* dlg = new FindDialog; 8 dlg->show(); 9 return a.exec(); 10 }
 1 //finddialog.h
 2 #ifndef FINDDIALOG_H
 3 #define FINDDIALOG_H
 4 
 5 #include <QDialog>
 6 #include<QLineEdit>
 7 #include<QCheckBox>
 8 #include<QPushButton>
 9 #include<QHBoxLayout>
10 #include<QVBoxLayout>
11 
12 class FindDialog : public QDialog
13 {
14     Q_OBJECT
15 public:
16     explicit FindDialog(QWidget *parent = 0);
17     ~FindDialog();
18 signals:
19 //其他槽函数    
20 public slots:
21     void    findBtnEnabled(const QString &txt);
22     void    findClicked();
23     void    stopClicked();
24 //查找槽函数
25 public    slots:
26     void    findPrevious_Slot(const QString&txt,Qt::CaseSensitivity cs);
27     void    findNext_Slot(const QString&txt,Qt::CaseSensitivity cs);
28 //查找窗口的部件
29 public:
30     QLineEdit*    text;
31     QCheckBox*    caseSensCheck;
32     QCheckBox*    prevCheck;
33     QPushButton*    findText;
34     QPushButton*    stopFind;
35 signals://自定义的查找信号
36      void findNext(const QString &str , Qt::CaseSensitivity cs);
37      void findPrevious(const QString &str , Qt::CaseSensitivity cs);
38 
39 public://查找窗口的布局
40     QHBoxLayout*    hLayout;
41     QVBoxLayout*    vLeftLayout;
42     QVBoxLayout*    vRightLayout;
43 };
44 
45 #endif // FINDDIALOG_H

posted on 2013-06-01 18:49  wowk  阅读(1899)  评论(0编辑  收藏  举报