对话框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

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QPushButton>
  5. class Dialog : public QDialog
  6. {
  7. Q_OBJECT
  8. public:
  9. Dialog(QWidget *parent = 0);
  10. ~Dialog();
  11. QPushButton *button1;
  12. QPushButton *button2;
  13. QPushButton *button3;
  14. };
  15. #endif // DIALOG_H

dialog.cpp

  1. #include "dialog.h"
  2. #include <QFileDialog>
  3. #include <QString>
  4. #include <QDebug>
  5. Dialog::Dialog(QWidget *parent)
  6. : QDialog(parent)
  7. {
  8. this->resize(400,400);
  9. button1 = new QPushButton("选择文件",this);
  10. button1->resize(80,80);
  11. button1->move(100,50);
  12. //
  13. connect(button1,&QPushButton::clicked,this,[](){
  14. QString str = QFileDialog::getOpenFileName();
  15. qDebug()<<str;
  16. });
  17. button2 = new QPushButton(this);
  18. button2->resize(50,50);
  19. button2->move(100,100);
  20. button2->setText("模态");
  21. connect(button2,&QPushButton::clicked,this,[](){
  22. QDialog dialog;
  23. dialog.setWindowTitle(tr("模态dialog!"));
  24. dialog.exec();
  25. });
  26. button3 = new QPushButton(this);
  27. button3->resize(50,50);
  28. button3->move(100,150);
  29. button3->setText("非模态");
  30. connect(button3,&QPushButton::clicked,this,[](){
  31. //如果不new,会一闪而过
  32. QDialog *dialog = new QDialog;
  33. //对话框关闭的时候,自动销毁对话框
  34. dialog->setAttribute(Qt::WA_DeleteOnClose);
  35. dialog->setWindowTitle(tr("非模态dialog"));
  36. dialog->show();
  37. });
  38. }
  39. Dialog::~Dialog()
  40. {
  41. }

四、消息对话框

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()类似,不同之处在于这个对话框提供一个黄色叹号图标

五、案例

登录框

  1. 新建项目Login(继承Dialog,勾选ui选项)
  2. 打开logindialog.ui对界面进行设计

image-20210128205521536.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>LoginDialog</class>
  4. <widget class="QDialog" name="LoginDialog">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>534</width>
  10. <height>410</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>登陆</string>
  15. </property>
  16. <layout class="QVBoxLayout" name="verticalLayout">
  17. <item>
  18. <spacer name="verticalSpacer">
  19. <property name="orientation">
  20. <enum>Qt::Vertical</enum>
  21. </property>
  22. <property name="sizeHint" stdset="0">
  23. <size>
  24. <width>20</width>
  25. <height>128</height>
  26. </size>
  27. </property>
  28. </spacer>
  29. </item>
  30. <item>
  31. <layout class="QGridLayout" name="gridLayout">
  32. <item row="1" column="0">
  33. <widget class="QLabel" name="label_2">
  34. <property name="font">
  35. <font>
  36. <pointsize>20</pointsize>
  37. </font>
  38. </property>
  39. <property name="text">
  40. <string>密 码:</string>
  41. </property>
  42. </widget>
  43. </item>
  44. <item row="0" column="1">
  45. <widget class="QLineEdit" name="m_editUserName"/>
  46. </item>
  47. <item row="0" column="0">
  48. <widget class="QLabel" name="label">
  49. <property name="font">
  50. <font>
  51. <pointsize>20</pointsize>
  52. </font>
  53. </property>
  54. <property name="layoutDirection">
  55. <enum>Qt::LeftToRight</enum>
  56. </property>
  57. <property name="text">
  58. <string>用户名:</string>
  59. </property>
  60. <property name="alignment">
  61. <set>Qt::AlignCenter</set>
  62. </property>
  63. </widget>
  64. </item>
  65. <item row="1" column="1">
  66. <widget class="QLineEdit" name="m_editPassword">
  67. <property name="echoMode">
  68. <enum>QLineEdit::Password</enum>
  69. </property>
  70. </widget>
  71. </item>
  72. </layout>
  73. </item>
  74. <item>
  75. <layout class="QHBoxLayout" name="horizontalLayout">
  76. <item>
  77. <spacer name="horizontalSpacer">
  78. <property name="orientation">
  79. <enum>Qt::Horizontal</enum>
  80. </property>
  81. <property name="sizeHint" stdset="0">
  82. <size>
  83. <width>40</width>
  84. <height>20</height>
  85. </size>
  86. </property>
  87. </spacer>
  88. </item>
  89. <item>
  90. <widget class="QDialogButtonBox" name="m_btnBox">
  91. <property name="layoutDirection">
  92. <enum>Qt::LeftToRight</enum>
  93. </property>
  94. <property name="standardButtons">
  95. <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
  96. </property>
  97. </widget>
  98. </item>
  99. <item>
  100. <spacer name="horizontalSpacer_2">
  101. <property name="orientation">
  102. <enum>Qt::Horizontal</enum>
  103. </property>
  104. <property name="sizeHint" stdset="0">
  105. <size>
  106. <width>40</width>
  107. <height>20</height>
  108. </size>
  109. </property>
  110. </spacer>
  111. </item>
  112. </layout>
  113. </item>
  114. <item>
  115. <spacer name="verticalSpacer_2">
  116. <property name="orientation">
  117. <enum>Qt::Vertical</enum>
  118. </property>
  119. <property name="sizeHint" stdset="0">
  120. <size>
  121. <width>20</width>
  122. <height>128</height>
  123. </size>
  124. </property>
  125. </spacer>
  126. </item>
  127. </layout>
  128. <zorder>m_btnBox</zorder>
  129. <zorder>m_editUserName</zorder>
  130. <zorder>m_editPassword</zorder>
  131. <zorder>label</zorder>
  132. <zorder>label_2</zorder>
  133. <zorder>horizontalSpacer</zorder>
  134. <zorder>horizontalSpacer_2</zorder>
  135. <zorder>verticalSpacer</zorder>
  136. <zorder>verticalSpacer_2</zorder>
  137. <zorder>m_btnBox</zorder>
  138. </widget>
  139. <layoutdefault spacing="6" margin="11"/>
  140. <resources/>
  141. <connections/>
  142. </ui>
  • 选中新建窗体,设置属性``` objectName : LoginDlg

windowTitle : 登录

  1. - 从工具箱中拖出"Label"部件放入窗体,设置属性

objectName : m_editUsername

text : 用户名:

alignment : AlignRight, AlignVCenter

  1. - 从工具箱中拖出"Label"部件放入窗体,设置属性

objectName : m_editPassword

text : 密码:

alignment : AlignRight, AlignVCenter

  1. - 从工具箱中拖出"Line Edit"部件放入窗体,设置属性<br />`objectName : m_editUsername`
  2. - 从工具箱中拖出"Line Edit"部件放入窗体,设置属性

objectName : m_editPassword

echoMode :Password

  1. - 从工具箱中拖出"Button Box"部件放入窗体,设置属性<br />`layoutDircetion : RightToLeft`<br />此时不必太刻意于对话框窗体及其子部件的位置和大小,只需粗略摆放到一个大概的相对位置即可
  2. - 点击窗体空白区域,同时选中“用户名:,密码:,及两个文本框”部件,选择”栅格布局",或使用快捷键`CTRL+G`对话框窗体中的子部件自动排列整齐
  3. - 点击窗体空白区域,选择`Button Box`部件,设置属性,`centerButtons` 点击打上对号,“Button Box”部件的位置自动调整至对话框窗体中间位置(或者在Button Box左右各添加一个`Horizontal Spacer`部件)
  4. - 从工具箱中拖出两个`Vertical Spacer`部件,分别放入用户名部件的上面和Button Box部件的下面,使用伸展器可以把其他部件挤压到中间固定的位置
  5. - 点击窗体空白区域,选择layout中的`layout Horizontally`,对话框窗体中的子部件自动沿垂直方向排列整齐
  6. 4. 编写`LoginDialog.h`头文件<br />自定义私有槽函数,`onAccepted`用来处理“确定”按钮,`onRejected`用来处理“取消”按钮```cpp
  7. #ifndef LOGINDIALOG_H
  8. #define LOGINDIALOG_H
  9. #include <QDialog>
  10. #include <QMessageBox> // 消息提示框
  11. #include <QDebug>
  12. namespace Ui {
  13. class LoginDialog;
  14. }
  15. class LoginDialog : public QDialog
  16. {
  17. Q_OBJECT
  18. public:
  19. explicit LoginDialog(QWidget *parent = 0);
  20. ~LoginDialog();
  21. public slots:
  22. //处理ok按钮的槽函数
  23. void onAccepted(void);
  24. //处理cancel按钮的槽函数
  25. void onRejected(void);
  26. private:
  27. Ui::LoginDialog *ui;
  28. };
  29. #endif // LOGINDIALOG_H
  1. 编写源程序文件`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; } ```

  1. 效果(丑爆了,程序员实现功能就好了,审美的活还是交给设计吧)

image-20210128211259975.png
Login.zip