代码实例
#include "modaldialog.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QDialog>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
ModalDialog::ModalDialog(QWidget *parent)
: QMainWindow(parent)
{
QMenuBar* mBar = this->menuBar();//类似于用new 创建一个菜单栏
this->setMenuBar(mBar);//为窗口添加一个菜单栏
QMenu* menu = mBar->addMenu("对话框");//为菜单栏添加一个菜单
QAction* modalDialog = menu->addAction("模态对话框");
connect(modalDialog,&QAction::triggered,[=](){
QDialog dlg;
dlg.exec();
qDebug() << "模态对话框";
});//点击模态对话框会创建一个模态对话框//
QAction* nomodalDialog = menu->addAction("非模态对话框");
connect(nomodalDialog,&QAction::triggered,[=](){
//this->dlg.show();
this->dlg = new QDialog();
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
qDebug()<<"2222222";
});
QAction* aboutDialog = menu->addAction("关于对话框");
connect(aboutDialog,&QAction::triggered,[=](){
QMessageBox::about(this,"about","关于Qt");
});
QAction* questionDialog = menu->addAction("问题对话框");
connect(questionDialog,&QAction::triggered,[=](){
int ret = QMessageBox::question(this,"question","Are you OK?",QMessageBox::Ok | QMessageBox::Cancel);
switch(ret)
{
case QMessageBox::Ok:
qDebug()<<" i am OK";
break;
case QMessageBox::Cancel:
qDebug()<<"I am not OK";
break;
default:
break;
}
});
QAction* fileDialog = menu->addAction("文件对话框");
connect(fileDialog,&QAction::triggered,[=](){
QString path = QFileDialog::getOpenFileName(this,"open","../","souce(*.cpp *.h);;"
"Text(*.txt);;all(*.*)");//带文件过滤器;
qDebug()<<path;
});
}
ModalDialog::~ModalDialog()
{
}