2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口

简介:  1 新建一个空项目 A 编写 .pro文件 QT += gui widgets   HEADERS += \     MyDialog.h   SOURCES += \     MyDialog.cpp B 编写MyDialog.h


1 新建一个空项目

A 编写 .pro文件

QT += gui widgets

 

HEADERS += \

    MyDialog.h

 

SOURCES += \

    MyDialog.cpp

B 编写MyDialog.h

#ifndef MYDIALOG_H

#define MYDIALOG_H

 

#include <QDialog>

 

class MyDialog:public QDialog

{

    Q_OBJECT

public:

    explicit MyDialog(QWidget *parent = 0);

 

    QString _strDir;

    void paintEvent(QPaintEvent *);

 

signals:

 

public slots:

    void slotButtonClick();

};

 

#endif // MYDIALOG_H

C  编写:MyDialog.cpp

#include "MyDialog.h"
#include <QPushButton>
#include <QDebug>
#include <QFileDialog>
#include <QFileInfo>
 
#include <QColorDialog>
#include <QFontDialog>
#include <QMessageBox>
#include <QPainter>
 
#include <QApplication>
 
MyDialog::MyDialog(QWidget *parent) :
    QDialog(parent)
{
    QPushButton* button = new QPushButton("Click me",this);
    connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClick()));
}
 
void MyDialog::slotButtonClick()
{
#if 0
    QDialog* dlg = new QDialog;
    int ret;
    QPushButton* button = new QPushButton(dlg);
    connect(button, SIGNAL(clicked()), dlg, SLOT(reject()));
 
    /*
     * 在模态对话框中,exec有自己的消息循环,并且把app的消息循环接管了
     * 如果Dialog是通过exec来显示,那么可以通过accepted或者rejected来关闭
     * 窗口,如果Dialog是通过show来显示,那么可以通过close来关闭窗口,
     * 这个和QWidget一样的
     *
     * 有许多特殊的dailog:文件选择,MessageBox,颜色选择,字体选择,打印预览,打印
     */
    ret = dlg->exec();
    if(ret == QDialog::Accepted)
    {
        qDebug() << "accepted";
    }
    if(ret == QDialog::Rejected)
    {
        qDebug() << "rejected";
}
//上面的运行结果如下:
#endif
#if 0
    //通过下面的方式打开保存文件
QString strFilename = QFileDialog::getSaveFileName(
NULL,
                          "Select file for save",
                          _strDir,
                          "pic file (*.png *.jpg)");
//运行结果:
#endif
#if 0 
    //打开一个文件
#endif
#if 0
     //选择一个存在的文件夹
    QString strFilename = QFileDialog::getExistingDirectory();
    if(strFilename.isEmpty())
    {
        qDebug() << "select none";
        return;
    }
 
    qDebug() << strFilename;
    QFileInfo fileInfo(strFilename);
    _strDir = fileInfo.filePath();
  
#endif
#if 0
//颜色选择框
QColorDialog color;
    color.exec();
QColor c = color.selectedColor();
#endif
#if 0
//字体选择器
QFontDialog fontDialog;
    fontDialog.exec();
QFont font = fontDialog.selectedFont();
#endif
#if 0
//MessageBox,消息提示窗口
    int ret = QMessageBox::question(this, "????", "realy do .......",
              QMessageBox::Yes| QMessageBox::No|
              QMessageBox::YesAll| QMessageBox::NoAll);
    if(ret == QMessageBox::Yes)
    {
         qDebug() << "user select yes";
    }
    if(ret == QMessageBox::No)
    {
         qDebug() << "user select no";
    }
#endif
}
 
void MyDialog::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.drawLine(QLine(0,0,200,200));
}
 
int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
 
    MyDialog dlg;
    dlg.show();
 
    return app.exec();
}

 

目录
相关文章
|
13天前
|
计算机视觉 数据格式
使用opencv在Qt控件上播放mp4文件
使用opencv在Qt控件上播放mp4文件
23 2
|
26天前
|
监控 安全 Linux
Qt 文件类实战:解锁文件操作的无限可能
Qt 文件类实战:解锁文件操作的无限可能
46 1
|
5月前
|
存储 Cloud Native Linux
C++ Qt关于启动可执行文件存在的问题
C++ Qt关于启动可执行文件存在的问题
|
5月前
|
存储 Cloud Native API
C++ QT监测可执行文件exe是否运行
C++ QT监测可执行文件exe是否运行
|
5月前
|
Linux iOS开发 MacOS
19 QT - 标准文件对话框
19 QT - 标准文件对话框
30 0
|
5月前
|
安全
04 QT - .pro文件
04 QT - .pro文件
32 0
|
26天前
|
存储 JSON C++
Qt cmake 增加qml文件:深度剖析Qt cmake 的qt_add_qml_module函数
Qt cmake 增加qml文件:深度剖析Qt cmake 的qt_add_qml_module函数
25 0
|
5月前
|
安全 测试技术 C++
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
最近用到了gRPC,配置了很长时间,分享一下配置过程。先来看一下我准备的文件包(资源我放在最后)
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
|
1月前
|
监控 C++
C++ Qt开发:QFileSystemWatcher文件监视组件
QFileSystemWatcher 是 Qt 框架中提供的一个类,用于监视文件系统中的文件和目录的变化。它允许你在文件或目录发生变化时接收通知,并可以用于监视文件的创建、删除、重命名以及内容修改等操作。这对于需要实时监控文件系统变化的应用程序是非常有用的。
33 5
C++ Qt开发:QFileSystemWatcher文件监视组件
|
2月前
|
IDE 开发工具
QT案例IDE编写 -- 新建和保存文件及退出程序
QT案例IDE编写 -- 新建和保存文件及退出程序
21 0