Qt learning 7 - modal and non-modal dialog boxes

Dialog boxes are divided into
modal dialog boxes (cannot operate on other windows)
non-modal dialog boxes (can operate on other windows)

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDialog"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->actionnew,&QAction::triggered,[=](){
        //模态对话框
        QDialog *dlg=new QDialog(this);
        dlg->resize(300,200);
        dlg->exec();//阻塞
        qDebug()<<"模态";
    });
}

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


Modal dialogs via

dlg->exec();//阻塞

1. Block to that line of code
insert image description here
At this time, run and click New without any output.
2.
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201014210736793.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MTU2MDIx,size_16,color_FFFFFF,t_70#pic_center
After closing, the following code will be executed to print out

Two, non-modal dialog box

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDialog"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->actionnew,&QAction::triggered,[=](){
    


        //非模态对话框
        QDialog *dlg2=new QDialog(this);
        dlg2->resize(300,200);
        dlg2->show();
        dlg2->setAttribute(Qt::WA_DeleteOnClose);
        qDebug()<<"非模态";




    });
}

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


after running
insert image description here

Guess you like

Origin blog.csdn.net/qq_45156021/article/details/109083880