//头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QDialog>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QDialog dlg;
};
#endif // MAINWINDOW_H
//源文件
#include "mainwindow.h"
#include<QMenuBar>
#include<QMenu>
#include<QPushButton>
#include<QAction>
#include<QDialog>
#include<QDebug>
#include<QMessageBox>
#include<QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->resize(800,800);
QMenuBar *mBar = menuBar();
setMenuBar(mBar);
QMenu *menu = mBar->addMenu(QStringLiteral("对话框"));
QAction *p1 = menu->addAction(QStringLiteral("模态对话框"));
connect(p1,&QAction::triggered,
[=]()
{
QDialog dlg1;
dlg1.exec();
qDebug()<<"11111";
}
);
QAction *p2 = menu->addAction(QStringLiteral("非模态对话框"));
connect(p2,&QAction::triggered,
[=]()
{
//方法一
dlg.show();
// //方法二
// QDialog *p = new QDialog(this);//指定父对象,跟父对象一起释放
// p->show();
// //方法三
// QDialog *p1 = new QDialog;
// p1->setAttribute(Qt::WA_DeleteOnClose);
// p1->show();
}
);
QAction *p3 = menu->addAction(QStringLiteral("关于对话框"));
connect(p3,&QAction::triggered,
[=]()
{
QMessageBox::about(this,QStringLiteral("消息框"),QStringLiteral("关于Qt"));
}
);
QAction *p4 = menu->addAction(QStringLiteral("问题对话框"));
connect(p4,&QAction::triggered,
[=]()
{
int ret = QMessageBox::question(this,QStringLiteral("问题"),QStringLiteral("Are you OK?"),QMessageBox::Ok | QMessageBox::Cancel);
switch(ret)
{
case QMessageBox::Ok:
{
qDebug()<<"I am OK";
break;
}
case QMessageBox::Cancel:
{
qDebug()<<"I am bad";
}
default:
break;
}
}
);
QAction *p5 = menu->addAction(QStringLiteral("文件对话框"));
connect(p5,&QAction::triggered,
[=]()
{
QString Path = QFileDialog::getOpenFileName(this,"open","../","souce(*.cpp *.h);;Text(*.txt);;all(*.*)");
qDebug()<<Path;
}
);
}
MainWindow::~MainWindow()
{
}
Qt--标准/问题对话框&&文件对话框
最新推荐文章于 2023-07-25 14:54:37 发布