模态对话框是指在没有关闭它之前,不能再与同一个应用程序的其他窗口进行交互,比如新建项目时弹出的对话框。
非模态对话框是指既可以和它交互,也可以与同一程序中的其他窗口交互,如word中查找替换对话框。
类源文件mywidget.cpp
代码一:
#include "mywidget.h"
#include "ui_mywidget.h"
#include<QDialog>
myWidget::myWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::myWidget)
{
ui->setupUi(this);
QDialog dialog(this);
dialog.show();
}
运行结果:dialog对话框闪现消失
代码二:
#include "mywidget.h"
#include "ui_mywidget.h"
#include<QDialog>
myWidget::myWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::myWidget)
{
ui->setupUi(this);
QDialog * dialog = new QDialog(this);
dialog->show();
}
运行结果:dialog对话框出现(非模态对话框)
代码三:(模态对话框)
#include "mywidget.h"
#include "ui_mywidget.h"
#include<QDialog>
myWidget::myWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::myWidget)
{
ui->setupUi(this);
QDialog dialog(this);
dialog.exec();
}
运行结果:
关闭这个对话框
对话框出现,但是mywidget窗口没有出现,关闭对话框,mywidget窗口弹出来了
总结:
代码二这种对话框叫非模态对话框,代码三这种对话框叫模态对话框
要想使一个对话框成为模态对话框,只需要调用它的exec()函数,而要使其成为非模态对话框,则使用new操作来创建,然后使用show()函数来显示。
注意:
使用new创建和show()显示也可以建立模态对话框,只需要在其前面使用setModa()函数即可
代码四:
QDialog * dialog = new QDialog(this);
dialog->setModal(true);
dialog->show();
两种模态对话框的区别:
代码三的其他窗口是在会话框关闭之后才会出现,而代码四的其他窗口是一起出现,但是不能进行交互