QT dialog 模态

来源:互联网 发布:电脑屏幕视频录像软件 编辑:程序博客网 时间:2024/06/11 00:21

对话框分为两种:1模态对话框 2非模态对话框

模态对话框就是阻塞对话框,就是你不解决掉这个对话框,谁没办法点这个对话框以外的操作的  (就平时点外面 “噔噔噔” 不让点那种样子的)

非模态就是自己弹出的对话框,我们可以不管他,继续进行当前窗口的任务。



其中模态对话框包含 应用程序级别和窗口级别  对应阻塞程度顾名思义了。

show()函数是非模态非阻塞性质的  show()的话要建立在堆上

exec()函数是模态阻塞性质的    exec()建立在堆栈上都可以 ,因为会阻塞,阻塞不会立刻在堆上释放掉




#include <QApplication>#include <QPushButton>#include <QDebug>#include<QDialog>#include<QMainWindow>#include<QTime>int main(int argc, char *argv[]){    QApplication app(argc, argv);    QMainWindow *DialogWindow= new QMainWindow ;    DialogWindow->setWindowTitle("A TSET WINDOW");      //建一个窗口    QPushButton *buttonshow = new QPushButton;                    //show()非模态(非阻塞)    QPushButton *buttonexec = new QPushButton;                    //exec() 模态(阻塞)    QPushButton *buttonshowinstack = new QPushButton;            //show() 建立在栈上 (会自动被释放)    buttonshow->setText("show");    buttonexec->setText("exec");    buttonshowinstack->setText("showinstack");    buttonshow->setGeometry(10,10,30,30);    buttonexec->setGeometry(50,10,30,30);    buttonshowinstack->setGeometry(10,50,80,30);  buttonshow->setParent(DialogWindow);  buttonexec->setParent(DialogWindow);  buttonshowinstack->setParent(DialogWindow);    QObject::connect(buttonshow, &QPushButton::clicked, [&](bool) {      //lambda         QDialog *m=new QDialog;        m->setWindowTitle("堆开辟 非阻塞");        m->setAttribute(Qt::WA_DeleteOnClose);          //当点击关闭对话框是 自动释放掉m对象        m->setGeometry(330,330,200,100);                //可以开很多Dialog 这样写重叠在一起了         m->show();        qDebug() << "buttonshow!";    });    QObject::connect(buttonshowinstack, &QPushButton::clicked, [&](bool) {        QDialog m;        m.setWindowTitle("栈开辟 非阻塞");        m.setGeometry(330,330,200,100);        m.show();  QTime reach = QTime::currentTime().addMSecs(600);  while(QTime::currentTime()<reach)    QCoreApplication::processEvents(QEventLoop::AllEvents,100);    //这三行用于延时 QT 貌似没有windows.h的Sleep函数        qDebug() << "buttonshowinstack!";    });    QObject::connect(buttonexec, &QPushButton::clicked, [&](bool) {        QDialog *m=new QDialog;        m->setWindowTitle(" 阻塞");         m->setGeometry(330,330,200,100);               m->setAttribute(Qt::WA_DeleteOnClose);            //当点击关闭对话框是 自动释放掉m对象        m->exec();        qDebug() << "buttonexec!";    });    DialogWindow->show();    buttonshow->show();    buttonexec->show();    buttonshowinstack->show();    return app.exec();}



点击show 就会出现非阻塞对话框  exec就会出现阻塞对话框  showinstack 一闪而过(延时了 不然看不到) 说明不能再栈上建立非阻塞对话框