• Qt学习17 对话框及其类型


    Qt学习17 对话框及其类型

    对话框的概念

    • 对话框是与用户进行简短交互的顶层窗口
    • QDialog是Qt中所有多画框窗口的基类
    • QDialog继承于QWidget是一种容器类型的组件

    在这里插入图片描述

    • QDialog的意义
      • QDialog作为一种专用的交互窗口而存在
      • QDialog不能作为子部件嵌入其他容器中
      • QDialog是定制了窗口式样的特殊的QWidget

    实验1 - QWidget和QDialog的区别

    #include "Dialog.h"
    #include <QApplication>
    #include <QWidget>
    #include <QDialog>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget widget;
        QDialog dialog(&widget);	// 虽然dialog设置了父组件,但是dialog依然会是顶层窗口
        dialog.show();
        dialog.setWindowTitle("I'am dialog");
        widget.show();
        widget.setWindowTitle("I'am widget");
    
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    #include <QApplication>
    #include <QWidget>
    #include <QDialog>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QDialog dialog;
        QWidget widget(&dialog);	// widget设置了父组件,widget会嵌入dialog中
        dialog.show();
        dialog.setWindowTitle("I'am dialog");
        widget.show();
        widget.setWindowTitle("I'am widget");
    
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    对话框的类型

    • 模态对话框( QDialog::exec() )

      • 显示后无法与父窗口进行交互
      • 是一种阻塞式的对话框调用方式
    • 非模态对话框 ( QDialog::show() )

      • 显示后独立存在可以同时与父窗口进行交互
      • 是一种非阻塞式的对话框调用方式
    • 一般情况

      • 模态对话框用于必须依赖用户选择的场合(80%)
      • 消息提示,文件选择,打印设置,等
    • 非模态对话框用于特殊功能设置的场合(20%)

      • 查找操作,属性设置,等
    • 小技巧

      • 上创建模态对话框是最简单常用的方式
      • 一般情况下非模态对话框需要在上创建
      • 通过 QDialog::setModel 函数可以创建混合特性的对话框
      • 非模态对话框需要指定 Qt::WA_DeleteOnClose 属性

    实验2 - 不同特性的对话框

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QPushButton>
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    protected:
        QPushButton ModalBtn;
        QPushButton NormalBtn;
        QPushButton MixedBtn;
    
    protected slots:
        void ModalBtn_clicked();
        void NormalBtn_clicked();
        void MixedBtn_clicked();
    
    public:
        Dialog(QWidget *parent = 0);
        ~Dialog();
    };
    
    #endif // DIALOG_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    #include "Dialog.h"
    #include <QDebug>
    // 模态对话框
    void Dialog::ModalBtn_clicked()
    {
        qDebug() << "ModalBtn_clicked() begin";
        QDialog dialog(this);
        dialog.exec();
        qDebug() << "ModalBtn_clicked() end";
    }
    // 非模态对话框
    void Dialog::NormalBtn_clicked()
    {
        qDebug() << "NormalBtn_clicked() begin";
        QDialog *dialog = new QDialog(this);	// 非模态对话框不会阻塞程序,必须放在堆上
        dialog->setAttribute(Qt::WA_DeleteOnClose);	// 为了防止内存泄漏,必须加上这行
        dialog->show();
        qDebug() << "NormalBtn_clicked() end";
    }
    // 混合属性对话框
    void Dialog::MixedBtn_clicked()
    {
        qDebug() << "MixedBtn_clicked() begin";
        QDialog *dialog = new QDialog(this);
        dialog->setAttribute(Qt::WA_DeleteOnClose);
        dialog->setModal(this);	// 混合属性对话框就是在非模态对话框的基础上
        dialog->show();
        qDebug() << "MixedBtn_clicked() end";
    }
    
    Dialog::Dialog(QWidget *parent)
        : QDialog(parent), ModalBtn(this), NormalBtn(this), MixedBtn(this)
    {
        ModalBtn.setText("Modal Dialog");
        ModalBtn.move(20, 20);
        ModalBtn.resize(100, 30);
    
        NormalBtn.setText("Normal Dialog");
        NormalBtn.move(20, 70);
        NormalBtn.resize(100, 30);
    
        MixedBtn.setText("Mixed Dialog");
        MixedBtn.move(20, 120);
        MixedBtn.resize(100, 30);
    
        connect(&ModalBtn, SIGNAL(clicked()), this, SLOT(ModalBtn_clicked()));
        connect(&NormalBtn, SIGNAL(clicked()), this, SLOT(NormalBtn_clicked()));
        connect(&MixedBtn, SIGNAL(clicked()), this, SLOT(MixedBtn_clicked()));
    
        resize(140, 170);
    }
    
    Dialog::~Dialog()
    {
        qDebug() << "~Dialog()";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    #include "Dialog.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog dlg;
        dlg.show();
    
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    对话框的返回值

    • 只有模态对话框才有返回值的概念
    • 模态对话框的返回值用于表示交互结果
    • QDialog::exec() 的返回值为交互结果
      • void QDialog::done(int i) 关闭对话框并将参数作为交互结果
      • QDialog::Accepted - 用户操作成功
      • QDialog::Rejected - 用户操作失败

    实验3 - 对话框的返回值

    #include "Dialog.h"
    #include <QDebug>
    
    void Dialog::ModalBtn_clicked()
    {
        qDebug() << "ModalBtn_clicked() begin";
        done(Accepted);
        qDebug() << "ModalBtn_clicked() end";
    }
    
    void Dialog::NormalBtn_clicked()
    {
        qDebug() << "NormalBtn_clicked() begin";
        done(Rejected);
        qDebug() << "NormalBtn_clicked() end";
    }
    
    void Dialog::MixedBtn_clicked()
    {
        qDebug() << "MixedBtn_clicked() begin";
        done(100);
        qDebug() << "MixedBtn_clicked() end";
    }
    
    Dialog::Dialog(QWidget *parent)
        : QDialog(parent), ModalBtn(this), NormalBtn(this), MixedBtn(this)
    {
        ModalBtn.setText("Modal Dialog");
        ModalBtn.move(20, 20);
        ModalBtn.resize(100, 30);
    
        NormalBtn.setText("Normal Dialog");
        NormalBtn.move(20, 70);
        NormalBtn.resize(100, 30);
    
        MixedBtn.setText("Mixed Dialog");
        MixedBtn.move(20, 120);
        MixedBtn.resize(100, 30);
    
        connect(&ModalBtn, SIGNAL(clicked()), this, SLOT(ModalBtn_clicked()));
        connect(&NormalBtn, SIGNAL(clicked()), this, SLOT(NormalBtn_clicked()));
        connect(&MixedBtn, SIGNAL(clicked()), this, SLOT(MixedBtn_clicked()));
    
        resize(140, 170);
    }
    
    Dialog::~Dialog()
    {
        qDebug() << "~Dialog()";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    #include "Dialog.h"
    #include <QApplication>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog dlg;
        int r = dlg.exec();
    
        if (r == QDialog::Accepted) {
            qDebug() << "QDialog::Accepted";
        }
        else if (r == QDialog::Rejected) {
            qDebug() << "QDialog::Rejected";
        }
        else {
            qDebug() << r;
        }
    
        return r;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    头文件使用实验2中的头文件。

    小结

    • 对话框分为模态对话框非模态对话框
    • 模态对话框是阻塞式的
    • 模态对话框用于以来用户交互结果的场合
    • 非模态对话框是非阻塞式的
    • 非模态对话框用于功能设置的场合
  • 相关阅读:
    Linux 标准IO
    月涨粉超150W,B站知识UP主是如何强势崛起?
    序列合并--优先队列的应用
    calibre和cpolar搭建一个私有的网络书库
    LCP 50. 宝石补给(每日一题)
    【Linux】CentOS8.4 安装docker
    由于没有远程桌面授权服务器可以提供许可证,远程会话连接已断开
    myj的尘歌壶
    UOS QTextEdit设置换行和滚动条(bug自动换行时右侧个别字符被遮盖)
    docker搭建fastdfs环境
  • 原文地址:https://blog.csdn.net/weixin_40743639/article/details/125530626