QT的对话框

本文详细介绍如何使用QT创建自定义对话框,包括类的继承、部件添加、信号与槽的连接及布局设置等关键步骤,并展示了如何通过lineEdit控制findButton的状态。

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

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

  1. 创建一个从QDialog继承的类。
  2. 添加对话框中的部件,如按钮等等
  3. 创建各种需要的消息和槽。
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}
这样一个对话框子类就建立好了,那么该实现了。

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

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}

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

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}
1好了整个对话框类就实现好了,调用来看看~
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。

    示例:

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距代码搞定

 

 

### QT 对话框使用方法 #### 1. Qt 标准对话框概述 Qt 提供了一组预定义的标准对话框,这些对话框都继承自 `QDialog` 类。它们可以用来实现常见的用户交互需求,例如显示消息、打开文件、选择颜色等功能[^1]。 #### 2. 常见的 Qt 对话框及其用法 ##### 2.1 `QMessageBox` `QMessageBox` 是一个常用的对话框类,用于向用户提供信息或请求确认操作。它可以通过静态函数轻松调用,而无需实例化对象。 以下是展示关于对话框的一个简单示例: ```cpp #include <QApplication> #include <QMessageBox> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 调用静态函数 about() 来显示关于对话框 QMessageBox::about(nullptr, "About", "This is a simple application using QMessageBox."); return app.exec(); } ``` 上述代码展示了如何通过 `QMessageBox::about()` 方法来创建并显示一个简单的关于对话框[^3]。 ##### 2.2 `QInputDialog` `QInputDialog` 可以让用户输入一些基本的数据类型,如字符串、整数、浮点数等。下面是一个获取用户输入字符串的例子: ```cpp #include <QApplication> #include <QInputDialog> #include <QDebug> int main(int argc, char *argv[]) { QApplication app(argc, argv); bool ok; QString text = QInputDialog::getText(nullptr, "Input Dialog", "Enter your name:", QLineEdit::Normal, "", &ok); if (ok && !text.isEmpty()) { qDebug() << "Your name is:" << text; } return app.exec(); } ``` 这段代码演示了如何利用 `QInputDialog::getText()` 获取用户的姓名输入[^4]。 #### 3. 总结 以上两个例子分别介绍了 `QMessageBox` 和 `QInputDialog` 的基础用法。实际开发中可以根据具体需求选用不同的对话框类型,并结合其他组件完成更复杂的交互逻辑。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值