#-------------------------------------------------
#
# Project created by QtCreator 2019-08-16T16:45:44
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 03_QDialog
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDialog>
#include <QMessageBox>
#include <QColorDialog>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//actionnew 属于QAction 类,它的
//点击新建菜单项,弹出对话框
//非模态,打开对话框后还可以对其他窗口做操作
//模态对话框,只能对相关一系列进程显示在最上层,不可以对其他窗口操作
connect(ui->actionnew,&QAction::triggered,this,[=](){
//对话框有两种
//模态对话框 非模态对话框
// QDialog dlg(this);
// dlg.exec();//阻塞,进入消息循环
// dlg.resize(200,200);
//非模态对话框
//QDialog dlg2(this);//创建到了栈上,一闪而过
// QDialog *dlg2=new QDialog(this);
// dlg2->resize(100,100);
// dlg2->show();
// //防止资源泄露,所以要设置属性,防止有人一直点下去。。。。
// //记住value 是 55的参数 Qt::WA_DeleteOnClose//关闭窗口释放指针
// dlg2->setAttribute(Qt::WA_DeleteOnClose);
//使用标准的对话框
//静态方法可以直接用类名调用
//错误对话框
QMessageBox::critical(this,"错误","critical");
//信息对话框
QMessageBox::information(this,"信息","info");
//比较关键,询问对话框
//五个参数:1,父亲 2,标题,3,提示内容, 4,按键类型,5,关联回车按键
//根据返回值来确定具体点了哪个按钮
if(QMessageBox::Save == QMessageBox::question(this,"问题","question",QMessageBox::Save|QMessageBox::Cancel,QMessageBox::Cancel)) //寻找默认的按钮文字
{
qDebug()<<"点击的保存";
}
else
{
qDebug()<<"点击的取消";
}
//警告对话框
QMessageBox::warning(this,"警告","warning!");
//颜色对话框
QColor color=QColorDialog::getColor(QColor(0,255,0));
qDebug()<<color.red()<<color.green()<<color.blue();
//所有属性都可以拿到手。
//文件对话框 参数1 父亲 2,提示 3,默认路径 ,4 可以过滤文件,但不会过滤文件夹
//会返回选取文件的路径
QString path=QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\dawnl\\Desktop\\","(*.txt *.png)");
qDebug()<<path;
qDebug()<<"弹出对话框";
});
}
MainWindow::~MainWindow()
{
delete ui;
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>文件</string>
</property>
<addaction name="actionnew"/>
<addaction name="actionopen"/>
</widget>
<addaction name="menu"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionnew"/>
<addaction name="actionopen"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionnew">
<property name="text">
<string>new</string>
</property>
</action>
<action name="actionopen">
<property name="text">
<string>open</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>