1、模态对话框
- 不可以对其他窗口进行操作,阻塞
- 创建窗口:
QDialog dlg(this);
- 阻塞:
dlg.exec();
//模态对话框
connect(ui->actionNew,&QAction::triggered,[=](){
QDialog dlg(this);
dlg.resize(300,100);
dlg.exec();//阻塞
});
2、非模态对话框
- 可以对其他窗口进行操作
- 堆区开辟:
QDialog *dlg2 = new QDialog(this);
- 显示:
dlg2->show();
- 设置关闭就释放 :
dlg2->setAttribute(Qt::WA_DeleteOnClose);
//非模态对话框
connect(ui->actionNew,&QAction::triggered,[=](){
//函数结束后,栈上的数据会被释放,因此窗口一闪而过
//QDialog dlg2(this);
//堆区开辟,但是在主窗口关闭前不能释放
QDialog *dlg2 = new QDialog(this);
dlg2->resize(300,100);
dlg2->show();
//设置关闭就释放
dlg2->setAttribute(Qt::WA_DeleteOnClose);
});