第八章 Qt GUI之对话框使用,qtgui


第八章 Qt GUI之对话框使用

对话框可以是模态(modal)的或非模态(modeless)两种。当我们在一个用户界面程序里面对一个对话框(比如选择文件对话框)的操作没有结束前,界面的其他窗口无法操作,遇到的这个对话框就是模态对话框,而当我们在一个字处理软件中利用查找和替换对话框时,可以在字处理软件和查找替换对话框之间切换进行交互,这就是个非模态对话框。

先来看一下QDialog类的继承关系,如下图所示。

 

 QDialog从QWidget继承,然后它下面又被Qt的内置对话框类(QFileDialog选择文件或目录对话框、QFontDialog选择字体对话框、QMessageBox消息提示对话框等)继承。用户要实现自己的对话框,需要继承自QDialog类,并包含头文件<QDialog>。

1、实现自己的对话框类

实现一个Find(查找对话框),它的运行效果如下图所示,这将实现一个拥有自主权的对话框。

程序清单如下:

finddialog.h

 1 #ifndef FINDDIALOG_H
 2 #define FINDDIALOG_H
 3 
 4 #include <QDialog>
 5 #include <QCheckBox>
 6 #include <QLabel>
 7 #include <QLineEdit>
 8 #include <QPushButton>
 9 
10 class FindDialog : public QDialog
11 {
12     Q_OBJECT
13     
14 public:
15     FindDialog(QWidget *parent = 0);
16     ~FindDialog();
17 
18 signals:
19     void findNext(const QString &str, Qt::CaseSensitivity cs);
20     void findPrevious(const QString &str, Qt::CaseSensitivity cs);
21 
22 private slots:
23     void findClicked();
24     void enableFindButton(const QString &text);
25 
26 private:
27     QLabel *label;
28     QLineEdit *lineEdit;
29     QCheckBox *caseCheckBox;
30     QCheckBox *backwardCheckBox;
31     QPushButton *findButton;
32     QPushButton *closeButton;
33 };
34 
35 #endif // FINDDIALOG_H

 

finddialog.cpp:

 1 #include <QtGui>
 2 #include "finddialog.h"
 3 
 4 FindDialog::FindDialog(QWidget *parent)
 5     : QDialog(parent)
 6 {
 7     label = new QLabel(tr("Find &what")); //tr()函数是把它们翻译成其他语言的标志,“&”来表示快捷键(Alt+W)
 8     lineEdit = new QLineEdit;
 9     label->setBuddy(lineEdit);//设置行编辑器为标签的伙伴,按下标签的快捷键(Alt+W)时接收焦点,焦点会移动到行编辑器
10 
11     caseCheckBox = new QCheckBox(tr("Math &case"));
12     backwardCheckBox = new QCheckBox(tr("Search &backward"));
13 
14     findButton = new QPushButton(tr("&Find"));
15     findButton->setDefault(true);//设置“Find”按钮为默认按钮,默认按钮就是当用户Enter键时能够按下对应的按钮
16     findButton->setEnabled(false);//禁用“Find”按钮,它通常显示为灰色,不能和用户进行交互操作
17 
18     closeButton = new QPushButton(tr("Close"));
19 
20     connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &)));
21     connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
22     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
23 
24     QHBoxLayout *topLeftLayout = new QHBoxLayout;
25     topLeftLayout->addWidget(label);
26     topLeftLayout->addWidget(lineEdit);
27 
28     QVBoxLayout *leftLayout = new QVBoxLayout;
29     leftLayout->addLayout(topLeftLayout);
30     leftLayout->addWidget(caseCheckBox);
31     leftLayout->addWidget(backwardCheckBox);
32 
33     QVBoxLayout *rightLayout = new QVBoxLayout;
34     rightLayout->addWidget(findButton);
35     rightLayout->addWidget(closeButton);
36     rightLayout->addStretch();
37 
38     QHBoxLayout *mainLayout = new QHBoxLayout;
39     mainLayout->addLayout(leftLayout);
40     mainLayout->addLayout(rightLayout);
41     setLayout(mainLayout); //将mainLayout布局安装在FindDialog
42 
43     setWindowTitle(tr("Find"));
44     setFixedHeight(sizeHint().height());
45 }
46 
47 void FindDialog::findClicked()
48 {
49     QString text = lineEdit->text();
50     Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
51     if (backwardCheckBox->isChecked())
52     {
53         emit findPrevious(text, cs);
54     }
55     else
56     {
57         emit findNext(text, cs);
58     }
59 }
60 
61 void FindDialog::enableFindButton(const QString &text)
62 {
63     findButton->setEnabled(!text.isEmpty());
64 }
65 
66 FindDialog::~FindDialog()
67 {
68     
69 }

main.cpp

#include <QApplication>
#include "finddialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FindDialog *w = new FindDialog;
    w->show();
    
    return a.exec();
}

编译运行就出现上图效果了。

2、使用Qt designer设置对话框,实现效果如下图:

相关内容

    暂无相关文章