QT 对话框
对话框 模态对话框(没有关闭不能操作其他窗口),非模态对话框
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog> //对话框
#include<QMessageBox>//消息对话框
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//使用添加QT资源文件 ": + 前缀名 + 文件名"
ui->actionNew->setIcon(QIcon(":/Images/New.png"));
ui->actionOpen->setIcon(QIcon(":/Images/open.png"));
//对话框 模态对话框(没有关闭不能操作其他窗口),非模态对话框
//模态创建 阻塞
connect(ui->actionNew,& QAction::triggered,[=](){
//模态创建 exec() 阻塞
QDialog dialog(this);
dialog.resize(200,100);
dialog.setWindowTitle("新建文件");
dialog.exec();
});
connect(ui->actionOpen,& QAction::triggered,[=](){
//非模态创建 show(),还要new
QDialog *dialogShow = new QDialog(this);
dialogShow->resize(200,100);
dialogShow->setWindowTitle("打开文件");
//解决对话框打开,关闭后要等到父窗口关闭后才会释放内存对象的问题。
//这样写后,只要对话框关闭内存就释放
dialogShow->setAttribute(Qt::WA_DeleteOnClose);
dialogShow->show();
});
//标准对话框
//消息对象框,错误
connect(ui->actionCritical,& QAction::triggered,[=](){
QMessageBox::critical(this,"错误对话框标题","这是一个错误对话框显示的错误内容");
});
//信息对话框
connect(ui->actionInfo,& QAction::triggered,[=](){
QMessageBox::information(this,"信息对话框标题","提示信息内容");
});
//问题对话框
connect(ui->actionQuest,& QAction::triggered,[=](){
//
//question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)
//QWidget *parent 指定父亲
//const QString &title 标题
//const QString &text 内容
//QMessageBox::StandardButtons buttons = StandardButtons(Yes | No) 在对话框中显示的按钮
//QMessageBox::StandardButton defaultButton = NoButton) 回车默认按钮
QMessageBox::StandardButton messageBoxResult = QMessageBox::question(this,"问题对话框标题","你确定要删除吗?", QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
if(messageBoxResult == QMessageBox::Yes)
{
qDebug() << "选择的是_Yes";
}
else
{
qDebug() << "选择的是_No";
}
});
connect(ui->actionWarning,& QAction::triggered,[=](){
QMessageBox::warning(this,"报警标题","报警内容");
});
}
MainWindow::~MainWindow()
{
delete ui;
}