Qt 19 standard dialog box-message dialog box QMessageBox, file selection dialog box QFileDialog

QT provides developers with some reusable dialog box types, these reusable dialog boxes all inherit from the QDialog class

The standard dialogs in QT follow the same usage:

/*定义对话框对象
DialogType 对话框类型
this ,指定当前对话框的父窗口
*/
DialogType dlg(this);

/* 设置对话框属性
*/
dlg.setPropertyXXX(value)

//以模态对框框的方式来使用
if(dlg.exec() == DialogType::Value)
{
//获取对话框数据
Type v = dlg.getDialogValue();

//处理对话框
//...
}

The message dialog box QMessageBox is used to prompt important program information

1 为用户提示重要消息
2 强制用户进行操作选择

The message dialog box uses:

//构造消息对话框对象,指定父窗口
QMessageBox msg(this)

//设置消息对话框的相关属性
msg.setWindowTitle("Message Title");//设置标题
msg.setText("This is message content"); // 设置提示消息
msg.setIcon(QMessageBox::Information); //设置图标 
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);//设置 按钮OK 按钮Cancel

// 以模态方式使用
if(msg.exec() == QMessageBox::Ok)
{
qDebug() << "Ok button is clicked!"
}

Utility functions in QMessageBox

QMessageBox::question
QMessageBox::information
QMessageBox::warning
QMessageBox::critical

QMessageBox::information
QMessageBox::information() can replace these lines of code:

QMessageBox msg(this);
msg.setWindowTitle("Window Title");//标题
msg.setText("This is a detail message dialog!");//显示消息内容
//设置图标,使用标准图标 通过 QMessageBox可以得到标准图标,使用Information这个图标
msg.setIcon(QMessageBox::Information);
//设置标准按钮 并显示三个按钮 OK,Cancel,YesToAll
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll);

The file selection dialog box QFileDialog is used to obtain the file path in the system.
Open Mode: the application requires the user to open an external file
Save Mode: the application needs to store the current content in the external file specified by the user

use:

//定义文件对话框,并指定父窗口
QFileDialog fd(this);

//设置属性,即 打开模式:AcceptOpen,保存模式:AcceptSave
fd.setAcceptMode(QFileDialog :: AcceptOpen);
//在打开模式下又分为两种模式: 选择单个文件(ExistingFile), 选择多个文件(ExistingFiles),
fd.setFileMode(QFileDialog ::ExistingFile);

// 以模态方式使用
if(fd.exec() == QFileDialog ::Accepted)
{
// 得到用户所选择的文件,使用 selectedFiles
//返回值为QStringList链表 链表中信息 即所选择的文件: 文件路径+名文件路径+名
QString fs = fd.selectedFiles();
}

Utility functions in QFileDialog

QFileDialog::getOpenFileName
QFileDialog::getOpenFileNames
QFileDialog::getSaveFileName

File type filter setFilter

In the file dialog box, you can define filters by file suffix.
Filter rules:

显示名(*.后缀1 *.后缀2 *.后缀3 ... *.后缀N)
如:
“Image(*.png *.xpm *.jpg)”
"Text(*.txt)"
"All(*.*)"

experiment

Widget.h

#ifndef _WIDGET_H_
#define _WIDGET_H_

#include <QtGui/QWidget>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT
private:
    QPushButton SimpleMsgBtn;//消息对话框
    QPushButton CustomMsgBtn;
    QPushButton OpenFileBtn;
    QPushButton SaveFileBtn;
private slots:
    void SimpleMsgBtn_Clicked();
    void CustomMsgBtn_Clicked();
    void OpenFileBtn_Clicked();
    void SaveFileBtn_Clicked();
public:
    Widget(QWidget *parent = 0);
    ~Widget();
};

#endif

Widget.cpp

#include "Widget.h"
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>


Widget::Widget(QWidget *parent) : QWidget(parent),
    SimpleMsgBtn(this), CustomMsgBtn(this), OpenFileBtn(this), SaveFileBtn(this)
{
    SimpleMsgBtn.setText("Simple Message Dialog");
    SimpleMsgBtn.move(20, 20);
    SimpleMsgBtn.resize(160, 30);

    CustomMsgBtn.setText("Custom Message Dialog");
    CustomMsgBtn.move(20, 70);
    CustomMsgBtn.resize(160, 30);

    OpenFileBtn.setText("Open File Dialog");
    OpenFileBtn.move(20, 120);
    OpenFileBtn.resize(160, 30);

    SaveFileBtn.setText("Save File Dialog");
    SaveFileBtn.move(20, 170);
    SaveFileBtn.resize(160, 30);

    resize(200, 220);
    setFixedSize(200, 220);

    connect(&SimpleMsgBtn, SIGNAL(clicked()), this, SLOT(SimpleMsgBtn_Clicked()));
    connect(&CustomMsgBtn, SIGNAL(clicked()), this, SLOT(CustomMsgBtn_Clicked()));
    connect(&OpenFileBtn, SIGNAL(clicked()), this, SLOT(OpenFileBtn_Clicked()));
    connect(&SaveFileBtn, SIGNAL(clicked()), this, SLOT(SaveFileBtn_Clicked()));
}

//创建消息对话框
void Widget::SimpleMsgBtn_Clicked()
{
    //创建消息对话框并指定父窗口是当前 Widget
    QMessageBox msg(this);

    msg.setText("This is a message dialog!");

    msg.exec();
}

//创建消息对话框 并设置图标和标准按钮
void Widget::CustomMsgBtn_Clicked()
{
    //创建消息对话框并指定父窗口是当前 Widget
    QMessageBox msg(this);

    msg.setWindowTitle("Window Title");//标题
    msg.setText("This is a detail message dialog!");//显示消息内容
    //设置图标,使用标准图标 通过 QMessageBox可以得到标准图标,使用Information这个图标
    msg.setIcon(QMessageBox::Information);
    //设置标准按钮 并显示三个按钮 OK,Cancel,YesToAll
    msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll);

    //以模态方式使用
    if( msg.exec() == QMessageBox::Ok )
    {
        qDebug() << "Ok button is clicked!";
    }
}

//打开模式的文件选择对话框
void Widget::OpenFileBtn_Clicked()
{
    //定义文件对话框,并指定父窗口
    QFileDialog dlg(this);

    //设置属性,即 打开模式:AcceptOpen,保存模式:AcceptSave
    dlg.setAcceptMode(QFileDialog::AcceptOpen);
    //文件过滤
    dlg.setFilter("Text(*.txt)");
    //模式为 选择多个文件(ExistingFiles),
    dlg.setFileMode(QFileDialog::ExistingFiles);

     //如果用户点击OK
    if( dlg.exec() == QFileDialog::Accepted )
    {
        // 得到用户所选择的文件,使用 selectedFiles 返回值为QStringList链表
        QStringList fs = dlg.selectedFiles();

        //打印链表中信息 即所选择的文件: 文件路径+名
        for(int i=0; i<fs.count(); i++)
        {
            qDebug() << fs[i];
        }
    }
}

//保存模式的文件选择对话框
void Widget::SaveFileBtn_Clicked()
{
    QFileDialog dlg(this);

    //设置属性,即 打开模式:AcceptOpen,保存模式:AcceptSave
    dlg.setAcceptMode(QFileDialog::AcceptSave);
    dlg.setFilter("Text(*.txt)");


    if( dlg.exec() == QFileDialog::Accepted )
    {
        QStringList fs = dlg.selectedFiles();

        for(int i=0; i<fs.count(); i++)
        {
            qDebug() << fs[i];
        }
    }
}

Widget::~Widget()
{
    
}

main.cpp

#include <QtGui/QApplication>
#include "Widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;

    w.show();
    
    return a.exec();
}

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/115001025