QT17-Dialog and its type QDialog

This article learns from the QT course of Tang Zuolin from Ditai Software.


QDialog is the base class of all dialog boxes in QT. QDialog inherits from QWidget. It is a container type component. QDialog objects can only exist as top-level windows and cannot be embedded in other containers as subcomponents. QDialog is a customized window style The special QWidget. The difference between QDialog and QWidget is that QDialog can only be used as the top-level window, while QWidget is not.

QDialog can create:

1 Modal dialog: a blocking dialog, created on the stack, cannot interact with the parent window after being displayed, and is always at the top level

QDialog::exec() ,创建模态对话框,注意在栈上创建 QDialog对象
只有模态对话框 有返回值概念,表示交互结果
QDialog::done(int i) :关闭对话框,并将参数作为交换结果
参数为:Accepted :用户操作成功
参数为:Rejected  :用户操作失败
具体值:其他

2 Non-modal dialog: non-blocking dialog, created on the heap, after displaying, you can interact with the parent window

QDialog* dialog = new QDialog(this); 注意在堆上创建 QDialog对象,并指定父子关系,指定父组件,这样组件会一直在父组件上
Qt::WA_DeleteOnClose :需要设置 属性 释放资
QDialog::show() 
可以与父组件交互,需要设置父组件this,才会一直位于父组件上面

3 Mixed state dialog box: The program is not blocked, but the dialog box is blocked, and cannot interact with the parent window after being displayed

Qt::WA_DeleteOnClose :需要设置 属性 释放资
       QDialog::setModal(true);//设置混合特性对话框属性,创建 混合态对话框。
       QDialog::show();

Experiment: QDialog modal, non-modal, and mixed-mode dialog use

head File:

#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui/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

Create modal dialogs, non-modal dialogs (non-blocking dialogs, mixed properties dialogs)

Dialog.cpp
#include "Dialog.h"
#include <QDebug>
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);
}
void Dialog::ModalBtn_Clicked()
{
    qDebug() << "ModalBtn_Clicked() Begin";
    /*
       QDialog dialog(this); 一般都是这样写,设定其父组件是主窗口,这样该组件就可以一直显示在其父组件上面。
       QDialog dialog;这样写也可以,因为此处将要创建的对话框dialog是 模态对话框,是阻塞式对话框,不论指不指定其父组件,效果都是模态对话框在顶层。
    */
    QDialog dialog(this);
    dialog.exec(); //创建模态对话框
    // done(Accepted);
    qDebug() << "ModalBtn_Clicked() End";
}
void Dialog::NormalBtn_Clicked()
{
    qDebug() << "NormalBtn_Clicked() Begin";
    /*
       非模态对话框 是非阻塞式对话框,如果在栈上创建,那么在对话框 dialog.show() 显示之后,不会阻塞停留,依旧会向下运行,程序运行结束,
       局部变量被释放,dialog被销毁,对话框一闪而逝。
    QDialog dialog;
    dialog.show();
     */
    /*
       非模态对话框 在堆上创建,避免一闪而过。
       dialog->setAttribute(Qt::WA_DeleteOnClose);设置对话框关闭释放资源属性,如果不设置该属性,则dialog指针会在程序结束后释放,而指向的空间被泄露。
        为非模态对话框设置 Qt::WA_DeleteOnClose 属性,会在对话框关闭时候 自己释放所占用的内存空间,避免内存泄漏
       new QDialog(this); 指定其父组件,使该对话框一直位于其父组件之上。这里不是阻塞式对话框,需要强制指定,否则父组件被点击时,会在该组件之上。
    */
    QDialog* dialog = new QDialog(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->show();
    // done(Rejected);
    qDebug() << "NormalBtn_Clicked() End";
}
void Dialog::MixedBtn_Clicked()
{
    qDebug() << "MixedBtn_Clicked() Begin";
    QDialog* dialog = new QDialog(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose);//非阻塞是对话框都需要设置 WA_DeleteOnClose属性
    //混合特性对话框特点:程序不阻塞,但是对话框阻塞,显示后无法与父窗口进行交互
    dialog->setModal(true);//设置混合特性对话框属性,创建 混合态对话框。
    dialog->show();
    // done(100);
    qDebug() << "MixedBtn_Clicked() End";
}
Dialog::~Dialog()
{
    qDebug() << "~Dialog()";
}

main.cpp

#include <QtGui/QApplication>
#include <QWidget>
#include <QDialog>
#include <QDebug>
#include "Dialog.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog dlg;

    dlg.show();//显示对话框

    return a.exec();
}

Insert picture description here

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/114906464