Qt学习之旅 - 模态和非模态对话框创建

模态对话框

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    //点击新建按钮,弹出一个对话框
    connect(ui->actionnew,QAction::triggered,[=](){
    
    
        //对话框
        //模态对话框(不可以对其他窗口进行操作)非模态对话框(可以对其他对话框进行操作)
        //模态创建
        QDialog dig(this);
        dig.resize(200,100);
        dig.exec();
        qDebug()<<"模态对话框弹出了";

    });
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}

在这里插入图片描述

非模态对话框

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    //点击新建按钮,弹出一个对话框
    connect(ui->actionnew,QAction::triggered,[=](){
    
    
        //对话框
        //模态对话框(不可以对其他窗口进行操作)非模态对话框(可以对其他对话框进行操作)
        //非模态创建
        QDialog *dig = new QDialog(this);
        #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    //点击新建按钮,弹出一个对话框
    connect(ui->actionnew,QAction::triggered,[=](){
    
    
        //对话框
        //模态对话框(不可以对其他窗口进行操作)非模态对话框(可以对其他对话框进行操作)
        //非模态创建
        QDialog *dig = new QDialog(this);
        dig->show();

    });
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}
        dig->show();

    });
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}

问题是点击创建新的对象,点久了可能会导致内存泄漏
在这里插入图片描述

connect(ui->actionnew,QAction::triggered,[=](){
    
    
    QDialog *dig = new QDialog(this);
    dig->resize(200,100);
    dig->show();
    dig->setAttribute(Qt::WA_DeleteOnClose);
});
  • 模态对话框就是创建后不能操作其他窗口
  • 非模态对话框就是创建后可以操作其他窗口

猜你喜欢

转载自blog.csdn.net/yasinawolaopo/article/details/131088371
今日推荐