QT的对话框

来源:互联网 发布:天下三冰心捏脸数据 编辑:程序博客网 时间:2024/05/16 11:17

QT的对话框类QDialog实现了对话框的基础,它从QWidget继承,可以提供一个返回值。

创建一个对话框的步骤如下:

  1. 创建一个从QDialog继承的类。
  2. 添加对话框中的部件,如按钮等等
  3. 创建各种需要的消息和槽。
    view source
    print?
    01class FindDialog : public QDialog                                   //从QDialog继承一个新的对话框类
    02{
    03    Q_OBJECT
    04public:
    05    FindDialog(QWidget *parent = 0);                                //在构造函数中将对对话框中所用到的部件进行创建初始化等等
    06signals:
    07    void findNext(const QString &str,Qt::CaseSensitivity cs);       //定义点击findButton时会发出的两个信号   
    08     void findPrevious(const QString &str,Qt::CaseSensitivity cs);
    09private slots:
    10    void findClicked();                                             //当findButton点击时触发槽
    11    void enableFindButton(const QString &text);                      //当lineEdit发生变坏是触发的槽   
    12private
    13    QLabel *label;                                                  //添加label
    14    QLineEdit *lineEdit;                        //添加lineEdit
    15    QCheckBox *caseCheckBox;                            //添加caseCheckBox                        
    16    QCheckBox *backwardCheckBox;                                    //添加backwardCheckBox
    17    QPushButton *findButton;                        //添加findButton
    18    QPushButton *closeButton;                           //添加closeButton 
    19}

 

这样一个对话框子类就建立好了,那么该实现了。

首先是在构造函数中创建对话框中的部件并设置,并且设计好部件在窗体中的布局。

view source
print?
01FindDialog::FindDialog(QWidget *parent)
02{
03    //tr()的作用之一是从.qm文件中取出与其所包含的内容对应的信息。
04    label = new QLabel(tr("Find &What:"));     //创建label
05    lineEdit = new QLineEdit;                 //创建lineEdit
06    //设置label的伙伴(Buddy),作用:当按下label快捷键时,lineEdit将获取到焦点
07    label->setBuddy(lineEdit);                
08    caseCheckBox = new QCheckBox;             //创建caseCheckBox
09    findButton = new QPushButton(tr("&Find")); //创建findButton并设置为默认按钮和不可用  
10    findButton->setDefault(true);
11    findButton->setEnabled(false);
12    closeButton = new QPushButton(tr("Close"));//创建findButton
13    //当lineEdit内容发生变化时将触发enableFindButton
14    connect(lineEdit,SIGNAL(textChanged(const QString &)),
15             this,SLOT(enableFindButton(const QString & )));
16    //点击findButton时触发findClicked()         
17    connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
18    //点击closeButton时,对话框关闭
19    connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
20    //以下创建布局管理对象,并设置好窗体中部件的布局。
21    QHBoxLayout *topLeftLayout = new QHBoxLayout;
22    topLeftLayout->addWidget(label);
23    topLeftLayout->addWidget(lineEdit);
24    QVBoxLayout *leftLayout = new QVBoxLayout;
25    leftLayout->addLayout(topLeftLayout);
26    leftLayout->addWidget(caseCheckBox);
27    leftLayout->addWidget(backwardCheckBox);
28    QVBoxLayout *rightLayout = new QVBoxLayout;
29    rightLayout->addWidget(findButton);
30    rightLayout->addWidget(closeButton);
31    rightLayout->addStretch();                //在布局中添加一个分隔符。
32    //将设置好的布局添加到主布局里
33    QHBoxLayout *mainLayout = new QHBoxLayout;
34    mainLayout->addLayout(leftLayout);
35    mainLayout->addLayout(rightLayout);
36    setLayout(mainLayout);                    //对话框的布局 
37    setWindowTitle(tr("Find"));               //设定对话标题   
38    setFixedHeight(sizeHint().height());     //设置对话框固定的高度。
39                                             //sizeHint().height()将返回一个理想的高度值     
40}

接着实现对话框中所用到的槽

view source
print?
01void FindDialog::findClicked()
02{
03    QString text = lineEdit->text();
04    //Qt::CaseSensitivity
05    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? 
06                        Qt::CaseSensitive : Qt::CaseInsensitive;
07    //根据选中的CheckBox发射不同的消息。                 
08    if(backwardCheckBox->isChecked())
09    {
10        emit findPrevious(text,cs);
11    }
12    else
13    {
14        emit findNext(text,cs);
15    }
16}
17//设置根据lineEdit内容设置findButton是否
18void FindDialog::enableFindButton(const QString &text)
19{
20    findButton->setEnabled(!text.isEmpty());
21}
view source
print?
1  
view source
print?
1好了真个对话框类就实现好了,调用来看看~
view source
print?
01#include <QApplication>
02#include <finddialog.h>
03  
04int main(int argc,char *argv[])
05{
06    QApplication app(argc,argv);
07    FindDialog *dialog = new FindDialog;
08    dialog->show();
09    return app.exec();
10}

 

运行如图:

Find

 

创建对话框的大致步骤就如上面的了。当然也可以通过QT Designer进行可视化的设计对话框后给程序调用。

关于lineEdit:

    lineEdit的输入可以通过检验器件来控制输入。如:QIntValidator,QDoubleValidator,QRegExpValidator。

    示例:

view source
print?
1QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
2lineEdit->setValidator(new QRegExpValidator(regExp,this));

这里限定lineEdit输入的第一个字符必须是字母,第二个字符必须是1-9的数字,然后可一个输入0至2个0到9的数字。

感觉这里正则真的很方便哦。以前写代码的时候控制edit数据总会写一堆代码,而QT这里2距代码搞定。。。。