QT模态和非模态对话框创建

QT模态和非模态对话框创建

1、模态对话框

  1. 不可以对其他窗口进行操作,阻塞
  2. 创建窗口:QDialog dlg(this);
  3. 阻塞:dlg.exec();
    //模态对话框
    connect(ui->actionNew,&QAction::triggered,[=](){
    
    
        QDialog dlg(this);
        dlg.resize(300,100);
        dlg.exec();//阻塞
    });

在这里插入图片描述

2、非模态对话框

  1. 可以对其他窗口进行操作
  2. 堆区开辟:QDialog *dlg2 = new QDialog(this);
  3. 显示:dlg2->show();
  4. 设置关闭就释放 :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);
    });

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43762434/article/details/133181977