对话框及其类型

对话框的概念

对话框是与用户进行简短交互的顶层窗口

QDialog 是 Qt 中所有对话框窗口的基类

QDialog 继承于 QWidget 是一种容器类型的组件

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

显示后无法与父窗口进行交互

是一种阻塞式的对话框调用方式

非模态对话框(QDialog::show())

显示后独立存在可以同时与父窗口进行交互

是一种非阻塞式的对话框调用方式

小技巧

在栈上创建模态对话框是最简单常用的方式

一般情况下非模态对话框需要在堆上创建

通过 QDialog::setModal 函数可以创建混合特性的对话框

非模态对话框需要指定 Qt::DeleteOnClose 属性

对话框的返回值

只有模态对话框才有返回值的概念

模态对话框的返回值用于表示交互结果

QDialog::exec() 的返回值为交互结果

void QDialog::done(int i) 关闭对话框并将参数作为交互结果

  • QDialog::Accepted - 用户操作成功
  • QDialog::Rejected - 用户操作失败

对话框

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

public slots:
    void ModalBtn_Clicked();
    void NormalBtn_Clicked();
    void MixedBtn_Clicked();
};
#endif // DIALOG_H

dialog.c

#include "dialog.h"
#include <QDebug>
#include <QPushButton>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    QPushButton *btn1 = new QPushButton(this);
    btn1->setText("Modal Dialog");
    btn1->resize(120, 50);
    btn1->move(50, 20);

    QPushButton *btn2 = new QPushButton(this);
    btn2->setText("Normal Dialog");
    btn2->resize(120, 50);
    btn2->move(50, 80);

    QPushButton *btn3 = new QPushButton(this);
    btn3->setText("Mixed Dialog");
    btn3->resize(120, 50);
    btn3->move(50, 140);

    connect(btn1, &QPushButton::clicked, this, &Dialog::ModalBtn_Clicked);
    connect(btn2, &QPushButton::clicked, this, &Dialog::NormalBtn_Clicked);
    connect(btn3, &QPushButton::clicked, this, &Dialog::MixedBtn_Clicked);
}

void Dialog::ModalBtn_Clicked()
{

    qDebug() << "ModalBtn_Clicked() Begin";

    QDialog dlg(this);
    dlg.exec();

    qDebug() << "ModalBtn_Clicked() End";

    done(Accepted);
}
void Dialog::NormalBtn_Clicked()
{

    qDebug() << "NormalBtn_Clicked() Begin";

    QDialog *dlg = new QDialog();
    dlg->setAttribute(Qt::WA_DeleteOnClose);
    dlg->show();

    qDebug() << "NormalBtn_Clicked() End";


    // done(Rejected);
}
void Dialog::MixedBtn_Clicked()
{

    qDebug() << "MixedBtn_Clicked() Begin";

    QDialog *dlg = new QDialog();
    dlg->setAttribute(Qt::WA_DeleteOnClose);
    dlg->setModal(true);
    dlg->show();

    qDebug() << "MixedBtn_Clicked() End";


    // done(100);
}

Dialog::~Dialog()
{
    qDebug() << "~Dialog()";
}

main.c

#include "dialog.h"

#include <QApplication>
#include <QPushButton>
#include <QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.resize(250, 200);
    int r = w.exec();

    if(r == QDialog::Accepted)
    {
        qDebug() << "Accepted";
    }
    else if(r == QDialog::Rejected)
    {
        qDebug() << "Rejected";
    }
    else
    {
        qDebug() << r;
    }

    return r;
}

在栈上创建的对话框,必须使用 exec 函数来展示,如果用 show 函数来展示的话,对话框会一闪而过;非模态对话框需要指定 Qt::DeleteOnClose 属性,否则过多的创建非模态对话框,堆空间的消耗会过大;done 函数会关闭对话框并将参数作为 exec 函数的交互结果,我们可以根据 exec 函数的返回值来完成具体的逻辑分支。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值