Qt中的QDialog类是所有对话框窗口类的基类。对话框通常被分为两类:模态对话框和非模态对话框。
模态对话框就是在没有关闭它之前,不能再与同一个程序的其他窗口进行交互的对话框。例如我们在Qt Creator中点击“将文件另存为”时弹出的对话框,在我们完成对文件的另存操作或者手动关闭这个窗口时,我们无法在Qt Creator中对其他窗口进行操作。
而对于非模态对话框,既可以与它交互的同时,也可以与同一程序中的其他窗口交互。
要创建模态对话框也很简单,只需要将对话框的属性设置为模态就行了,例如以下程序
#include "dialog.h"
#include "ui_dialog.h"
#include<QDialog>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
QDialog * dialog = new QDialog(this); //创建一个对话框
dialog->setModal(true); //设置模态属性
dialog->show(); //显示对话框
}
Dialog::~Dialog()
{
delete ui;
}