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
At this time, run and click New without any output.
2.
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