[QT] Explain in detail the difference between exec and open in QDialog

1. The difference between the two

注意需要看下方的打印信息

insert image description here

2. Partial code explanation

2.1 open display dialog

2.1.1 Object creation

QDialog dia;
dia.resize(400, 300);
dia.open();
qDebug() << "111";

Use the object to display a dialog box, and use open to display it, that is, the window realized by the button Btn1. You can clearly see that the dialog window is flashed, but it does not affect the operation of the code after the open. Its "111" will still print out.
为什么一闪而过?
The object dia at this time is a local object, and the local object is stored in the stack , constructed in the function body, and destroyed when the function ends. Its lifetime is the entire function. That is to say, when the function ends, its object dia has been destroyed, so the window is gone.

2.1.2 Pointer creation

QDialog* pDia = new QDialog(this);
pDia->resize(400, 300);
pDia->open();
qDebug() << "222";

Use the pointer to realize the dialog box, use open to display, that is, the window realized by the button Btn2, you can see that the dialog box is modal, but it does not affect the operation of the code after the open, that is, the printing of "222".
If the pointer is used, there will be no flashing situation, because it is stored in the heap and needs to be released manually.

2.2 exec display dialog box

//指针方式
QDialog* pDia = new QDialog(this);
pDia->resize(400, 300);
pDia->exec();
qDebug() << "333";
//对象方式
QDialog dia;
dia.resize(400, 300);
dia.exec();
qDebug() << "333";

Use exec to display, that is, the window realized by the button Btn3, you can see that the dialog box is modal, and after the dialog box is displayed, the code behind it will not be executed, that is, "333" will not be printed, that is It is said that the window displayed by exec will always block the following code and will not execute until the user closes it, and the following code will not be executed. At this time, "333" will be printed.
Regardless of whether the dialog box is implemented by using an object or a pointer , the code behind it will not be executed and will always be blocked there.
If it is an object, exec will block, so that the entire function will not be executed, and the object will not be destructed.

3. The difference between exec and open

  • open: display 模态窗口, will 继续执行open the code after
  • exec: display 模态窗口, yes 一直阻塞, the code behind exec will be blocked and will not be executed, 直到用户关闭它and the code behind will be executed

4. All code samples

4.1 MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QMainWindow>
#include <QPushButton>

namespace Ui {
    
    
    class MainWindow;
}

class MainWindow : public QMainWindow {
    
    
    Q_OBJECT

public:
    explicit MainWindow(QWidget* parent = 0);
    ~MainWindow();

private slots:
    void slotShowDia1();
    void slotShowDia2();
    void slotShowDia3();

private:
    Ui::MainWindow* ui;

    QPushButton* m_pBtn1;
    QPushButton* m_pBtn2;
    QPushButton* m_pBtn3;
};

#endif  // MAINWINDOW_H

4.2 MainWindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    
    
    ui->setupUi(this);
    this->resize(800, 600);

    QPushButton* m_pBtn1 = new QPushButton(QStringLiteral("Btn1"), this);
    QPushButton* m_pBtn2 = new QPushButton(QStringLiteral("Btn2"), this);
    QPushButton* m_pBtn3 = new QPushButton(QStringLiteral("Btn3"), this);

    QWidget* widget = this->centralWidget();
    QHBoxLayout* lay = new QHBoxLayout(widget);
    lay->addWidget(m_pBtn1);
    lay->addWidget(m_pBtn2);
    lay->addWidget(m_pBtn3);

    connect(m_pBtn1, &QPushButton::clicked, this, &MainWindow::slotShowDia1);
    connect(m_pBtn2, &QPushButton::clicked, this, &MainWindow::slotShowDia2);
    connect(m_pBtn3, &QPushButton::clicked, this, &MainWindow::slotShowDia3);
}

MainWindow::~MainWindow() {
    
     delete ui; }

void MainWindow::slotShowDia1() {
    
    
    QDialog dia;
    dia.resize(400, 300);
    dia.open();
    qDebug() << "111";
}

void MainWindow::slotShowDia2() {
    
    
    QDialog* pDia = new QDialog(this);
    pDia->resize(400, 300);
    pDia->open();
    qDebug() << "222";
}

void MainWindow::slotShowDia3() {
    
    
	//指针方式
    QDialog* pDia = new QDialog(this);
    pDia->resize(400, 300);
    pDia->exec();
    qDebug() << "333";
    
	//对象方式
	//QDialog dia;
	//dia.resize(400, 300);
	//dia.exec();
	//qDebug() << "333";
}

Guess you like

Origin blog.csdn.net/WL0616/article/details/129704189