对话框QDialog
一、概念
- 对话框是GUI程序中不可或缺的组成部分。
- 很多不能或者不适合放入主窗口的功能组件都必须放在对话框中设置。
- 对话框通常回是一个顶层窗口,出现在程序最上层,用于实现短期任务或者简洁的用户交互。
- Qt中使用QDialog类实现对话框,我们通常会设计一个类继承QDialog。
- QDialog及其子类,以及所有Qt::Dialog类型的类,对于其parent指针都有额外的解释:
- 如果parent位Null,则该对话框会作为一个顶层窗口
- 否则则作为其父组件的子对话框,默认出现的位置是parent的中心
顶层窗口与非顶层窗口的区别在于:
- 顶层窗口在任务栏会有自己的位置;
- 非顶层窗口则会共享其父组件的位置。
对话框分为模态对话框和非模态对话框:
- 模态对话框,就是会阻塞同一应用程序中其他窗口的输入。
如:“打开文件”功能,当打开文件对话框出现时,我们是不能对除此对话框之外的窗口进行操作的 - 非模态对话框与模态对话框相反。
如:查找对话框,我们可以在显示着查找对话框的同时,继续对文档的内容进行编辑。
二、标准对话框
Qt内置的一系列对话框,用于简化开发。
Qt内置对话框大致分类:
| 类 | 功能 |
|---|---|
| QColorDialog | 选择颜色 |
| QFileDialog | 选择文件或者目录 |
| QFontDialog | 选择字体 |
| QInputDialog | 允许用户输入一个值,并将其返回 |
| QMessageDialog | 模拟对话框,用于显示信息、寻问问题等 |
| QPageSetupDialog | 为打印机提供纸张相关的选项 |
| QPrintDialog | 打印机配置 |
| QPrintPreviewDialog | 打印预览 |
| QProgressDialog | 显示操作过程 |
三、自定义消息框
Qt支持模态对话框和非模态对话框
模态与非模态的实现:
- 使用
QDialog::exec()实现应用程序级别的模态对话框 - 使用
QDialog::open()实现窗口级别的模态对话框 - 使用
QDialog::show()实现非模态对话框
模态对话框
- 应用程序级别:当这种模态的对话框出现的时候,用户必须首先对对话框进行交互,知道关闭对话框,然后才能访问程序中其他的窗口。
- 窗口级别:该模态仅仅阻塞与对话框关联的窗口,但是依然允许用户与程序中其他窗口交互。窗口级别的模态尤其适用于对窗口模式。
- 一般默认应用程序级别是模态。
案例代码如下:
dialog.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include <QPushButton>class Dialog : public QDialog{Q_OBJECTpublic:Dialog(QWidget *parent = 0);~Dialog();QPushButton *button1;QPushButton *button2;QPushButton *button3;};#endif // DIALOG_H
dialog.cpp
#include "dialog.h"#include <QFileDialog>#include <QString>#include <QDebug>Dialog::Dialog(QWidget *parent): QDialog(parent){this->resize(400,400);button1 = new QPushButton("选择文件",this);button1->resize(80,80);button1->move(100,50);//connect(button1,&QPushButton::clicked,this,[](){QString str = QFileDialog::getOpenFileName();qDebug()<<str;});button2 = new QPushButton(this);button2->resize(50,50);button2->move(100,100);button2->setText("模态");connect(button2,&QPushButton::clicked,this,[](){QDialog dialog;dialog.setWindowTitle(tr("模态dialog!"));dialog.exec();});button3 = new QPushButton(this);button3->resize(50,50);button3->move(100,150);button3->setText("非模态");connect(button3,&QPushButton::clicked,this,[](){//如果不new,会一闪而过QDialog *dialog = new QDialog;//对话框关闭的时候,自动销毁对话框dialog->setAttribute(Qt::WA_DeleteOnClose);dialog->setWindowTitle(tr("非模态dialog"));dialog->show();});}Dialog::~Dialog(){}
四、消息对话框
QMessageBox类提供了一个模式对话框,用于通知用户或询问用户问题并接收答案。
静态函数:
返回值类型 函数名(参数)
void about(QWidget *parent, const QString &title, const QString &text)
显示关于对话框。
一个最简单的对话框,显示带有标题和文本的简单“关于”框,“关于”框的父对象是父对象;
标题是title,内容是text,父窗口是parent;
对话框只有一个OK按钮。void QMessageBox::aboutQt([QWidget](qwidget.html#QWidget) parent, const [QString](../qtcore/qstring.html) &title = QString())
显示关于Qt对话框,用于显示有关Qt的信息StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
显示严重错误对话框,这个对话框将显示一个红色的错误符号,我们可以通过buttons参数指明其显示的按钮。
默认情况下只有一个OK按钮,我们可以使用StandardButtons类型指定多种按钮StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
与QMessageBox::critical()类似,不同之处在于这个对话框提供一个普通信息图标StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton)
与QMessageBox::critical()类似,不同之处在于这个对话框提供一个问号图标,并且其显示的按钮是“是”和“否”StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
与QMessageBox::critical()类似,不同之处在于这个对话框提供一个黄色叹号图标
五、案例
登录框
- 新建项目Login(继承Dialog,勾选ui选项)
- 打开
logindialog.ui对界面进行设计

<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"><class>LoginDialog</class><widget class="QDialog" name="LoginDialog"><property name="geometry"><rect><x>0</x><y>0</y><width>534</width><height>410</height></rect></property><property name="windowTitle"><string>登陆</string></property><layout class="QVBoxLayout" name="verticalLayout"><item><spacer name="verticalSpacer"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>128</height></size></property></spacer></item><item><layout class="QGridLayout" name="gridLayout"><item row="1" column="0"><widget class="QLabel" name="label_2"><property name="font"><font><pointsize>20</pointsize></font></property><property name="text"><string>密 码:</string></property></widget></item><item row="0" column="1"><widget class="QLineEdit" name="m_editUserName"/></item><item row="0" column="0"><widget class="QLabel" name="label"><property name="font"><font><pointsize>20</pointsize></font></property><property name="layoutDirection"><enum>Qt::LeftToRight</enum></property><property name="text"><string>用户名:</string></property><property name="alignment"><set>Qt::AlignCenter</set></property></widget></item><item row="1" column="1"><widget class="QLineEdit" name="m_editPassword"><property name="echoMode"><enum>QLineEdit::Password</enum></property></widget></item></layout></item><item><layout class="QHBoxLayout" name="horizontalLayout"><item><spacer name="horizontalSpacer"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QDialogButtonBox" name="m_btnBox"><property name="layoutDirection"><enum>Qt::LeftToRight</enum></property><property name="standardButtons"><set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set></property></widget></item><item><spacer name="horizontalSpacer_2"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item></layout></item><item><spacer name="verticalSpacer_2"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>128</height></size></property></spacer></item></layout><zorder>m_btnBox</zorder><zorder>m_editUserName</zorder><zorder>m_editPassword</zorder><zorder>label</zorder><zorder>label_2</zorder><zorder>horizontalSpacer</zorder><zorder>horizontalSpacer_2</zorder><zorder>verticalSpacer</zorder><zorder>verticalSpacer_2</zorder><zorder>m_btnBox</zorder></widget><layoutdefault spacing="6" margin="11"/><resources/><connections/></ui>
- 选中新建窗体,设置属性``` objectName : LoginDlg
windowTitle : 登录
- 从工具箱中拖出"Label"部件放入窗体,设置属性
objectName : m_editUsername
text : 用户名:
alignment : AlignRight, AlignVCenter
- 从工具箱中拖出"Label"部件放入窗体,设置属性
objectName : m_editPassword
text : 密码:
alignment : AlignRight, AlignVCenter
- 从工具箱中拖出"Line Edit"部件放入窗体,设置属性<br />`objectName : m_editUsername`- 从工具箱中拖出"Line Edit"部件放入窗体,设置属性
objectName : m_editPassword
echoMode :Password
- 从工具箱中拖出"Button Box"部件放入窗体,设置属性<br />`layoutDircetion : RightToLeft`<br />此时不必太刻意于对话框窗体及其子部件的位置和大小,只需粗略摆放到一个大概的相对位置即可- 点击窗体空白区域,同时选中“用户名:,密码:,及两个文本框”部件,选择”栅格布局",或使用快捷键`CTRL+G`对话框窗体中的子部件自动排列整齐- 点击窗体空白区域,选择`Button Box`部件,设置属性,`centerButtons` 点击打上对号,“Button Box”部件的位置自动调整至对话框窗体中间位置(或者在Button Box左右各添加一个`Horizontal Spacer`部件)- 从工具箱中拖出两个`Vertical Spacer`部件,分别放入用户名部件的上面和Button Box部件的下面,使用伸展器可以把其他部件挤压到中间固定的位置- 点击窗体空白区域,选择layout中的`layout Horizontally`,对话框窗体中的子部件自动沿垂直方向排列整齐4. 编写`LoginDialog.h`头文件<br />自定义私有槽函数,`onAccepted`用来处理“确定”按钮,`onRejected`用来处理“取消”按钮```cpp#ifndef LOGINDIALOG_H#define LOGINDIALOG_H#include <QDialog>#include <QMessageBox> // 消息提示框#include <QDebug>namespace Ui {class LoginDialog;}class LoginDialog : public QDialog{Q_OBJECTpublic:explicit LoginDialog(QWidget *parent = 0);~LoginDialog();public slots://处理ok按钮的槽函数void onAccepted(void);//处理cancel按钮的槽函数void onRejected(void);private:Ui::LoginDialog *ui;};#endif // LOGINDIALOG_H
- 编写源程序文件`LoginDlg.cpp````cpp
include “logindialog.h”
include “ui_logindialog.h”
include
LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent), ui(new Ui::LoginDialog) { //初始化界面 ui->setupUi(this); //Ok—>确定 ui->m_btnBox->button(QDialogButtonBox::Ok)->setText(“确定”); // Cancel —>取消 ui->m_btnBox->button(QDialogButtonBox::Cancel)->setText(“取消”); // 连接点击按钮产生的accepted和rejected信号和当前窗口的处理“确认”和“取消”的槽函数 // 点击Ok 发送信号 accepted connect(ui->m_btnBox,SIGNAL(accepted()),this,SLOT(onAccepted())); // 点击Cancel 发送信号 rejected connect(ui->m_btnBox,SIGNAL(rejected()),this,SLOT(onRejected()));
} / 处理Ok按钮点击的槽函数 该函数处理”确认”按钮的槽函数,当用户名和密码编辑框输入的都是“qiu/wenbin”,则关闭该窗口(或显示主窗口界面,自己实现)。否则弹出消息框,提示“用户名或密码错误,登录失败”,点击“确认”返回登录界面。 / void LoginDialog::onAccepted(void){ qDebug(“登陆!”); //判断登陆用户名密码 if(ui->m_editUserName->text()==”qiu”&&ui->m_editPassword->text()==”wenbin”){ qDebug() << “登录成功”; // 关闭登录窗口 close(); } else { QMessageBox msgBox( QMessageBox::Critical, // 图标 “提示”, // 标题栏文本 “登录名或密码错误”, // 提示信息 QMessageBox::Ok, // 按钮 this // 父窗口 ); msgBox.setButtonText(QMessageBox::Ok,”确定”); // 显示消息提示框 msgBox.exec(); } } / 处理Cancel按钮点击的槽函数 该函数处理”取消”按钮的槽函数,当用户点击取消时,弹出消息对话框,提示用户“您真的要取消登录吗?”,并让用户做出选择。选择“Yes”则关闭该窗口。“No”则返回继续登录。 / void LoginDialog::onRejected(void){ qDebug(“取消”); QMessageBox msgBox( QMessageBox::Question,”登陆”,”你确定取消吗?”,QMessageBox::Yes|QMessageBox::No,this ); msgBox.setButtonText(QMessageBox::Yes,”是”); msgBox.setButtonText(QMessageBox::No,”否”); if(msgBox.exec()==QMessageBox::Yes){ close(); } }
LoginDialog::~LoginDialog() { delete ui; } ```
- 效果(丑爆了,程序员实现功能就好了,审美的活还是交给设计吧)

