QT实现QMessageBox中文按钮

这是我记录Qt学习过程心得文章的第二篇,主要是为了方便QMessageBox弹出框的使用,通过自定义的方式,将其常用的功能,统一封装成一个函数,还是写在了Skysonya类里面。

实现代码:

cpp 复制代码
//中文提示对话框
bool Skysonya::messageBox(QString msgType, QString dlgTitle, QString strInfo)
{
    QMessageBox *msgBox;
    int result = -1;
    bool value = false;
    if (msgType.toLower() == "critical")
    {
        //创建一个Critical弹出对话框,添加按钮:"Ok"
        msgBox = new QMessageBox(QMessageBox::Critical, dlgTitle, strInfo, QMessageBox::Ok);
        msgBox->button(QMessageBox::Ok)->setText("确定");  //将"Ok"按钮改为显示"确定"
        result = msgBox->exec();                           //显示Critical弹出对话框
        if (result == QMessageBox::Ok)
        {
            value = true;
        }
    }
    else if (msgType.toLower() == "warning")
    {
        //创建一个Warning弹出对话框,添加按钮:"Ok"
        msgBox = new QMessageBox(QMessageBox::Warning, dlgTitle, strInfo, QMessageBox::Ok);
        msgBox->button(QMessageBox::Ok)->setText("确定");  //将"Ok"按钮改为显示"确定"
        result = msgBox->exec();                           //显示Warning弹出对话框
        if (result == QMessageBox::Ok)
        {
            value = true;
        }
    }
    else if (msgType.toLower() == "warningyes")
    {
        //创建一个Warning弹出对话框,添加按钮:"Yes"、"No"
        msgBox = new QMessageBox(QMessageBox::Warning, dlgTitle, strInfo, QMessageBox::Yes | QMessageBox::No);
        msgBox->button(QMessageBox::Yes)->setText("是");  //将"Yes"按钮改为显示"是"
        msgBox->button(QMessageBox::No)->setText("否");   //将"No"按钮改为显示"否"
        result = msgBox->exec();                          //显示Warning弹出对话框
        if (result == QMessageBox::Yes)
        {
            value = true;
        }
    }
    else if (msgType.toLower() == "information")
    {
        //创建一个Information弹出对话框,添加按钮:"Ok"
        msgBox = new QMessageBox(QMessageBox::Information, dlgTitle, strInfo, QMessageBox::Ok);
        msgBox->button(QMessageBox::Ok)->setText("确定");  //将"Ok"按钮改为显示"确定"
        result = msgBox->exec();                           //显示Information弹出对话框
        if (result == QMessageBox::Ok)
        {
            value = true;
        }
    }
    else if (msgType.toLower() == "informationyes")
    {
        //创建一个Information弹出对话框,添加按钮:"Ok"
        msgBox = new QMessageBox(QMessageBox::Information, dlgTitle, strInfo, QMessageBox::Yes | QMessageBox::No);
        msgBox->button(QMessageBox::Yes)->setText("是");  //将"Yes"按钮改为显示"是"
        msgBox->button(QMessageBox::No)->setText("否");   //将"No"按钮改为显示"否"
        result = msgBox->exec();                          //显示Information弹出对话框
        if (result == QMessageBox::Yes)
        {
            value = true;
        }
    }
    return value;
}

具体使用:

cpp 复制代码
//文件
void MainWindow::slot_file_triggered()
{

    //输入对话框
    QString strInfo = skysonya.inputDialog("对话框", "请输入:");
    skysonya.messageBox("warning", "输入对话框", strInfo);
}
自定义类:
cpp 复制代码
#ifndef SKYSONYA_H
#define SKYSONYA_H
#include <QDebug>
#include <QFile>
#include <QInputDialog>
#include <QMessageBox>
#include <QObject>
#include <QPushButton>
#include <QString>
#include <QTextCodec>

enum EncodingFormat
{
    ANSI,
    UTF16LE,
    UTF16BE,
    UTF8,
    UTF8BOM,
};

class Skysonya : public QObject
{
    Q_OBJECT
    Q_ENUM(EncodingFormat)
public:
    explicit Skysonya(QObject *parent = nullptr);
    ~Skysonya();
    QString doAppAbout(QString appName);                                               //程序关于信息
    bool messageBox(QString msgType, QString dlgTitle, QString strInfo);               //中文提示对话框
    QString inputDialog(QString dlgTitle, QString labelText, QString textValue = "");  //中文按钮文本输入对话框
    QTextCodec *getFileCharacterEncoding(const QString &fileName);                     //获取文件编码格式函数
    QString openFileByIOWhole(const QString &fileName);                 //用QFile打开文件,整体读取
    QString openFileByIOLines(const QString &fileName);                 //用QFile打开文件,逐行读取
    QString openFileByStreamWhole(const QString &fileName);             //用QTextStream读取文件,整体读取
    QString openFileByStreamLines(const QString &fileName);             //用QTextStream读取文件,逐行读取
    bool saveFileByIOWhole(const QString &fileName, QString text);      //用QFile保存文件,整体保存
    bool saveFileByStreamWhole(const QString &fileName, QString text);  //用QTextStream保存文件,整体保存

private:
    QString appVersion;       //软件版本号
    QString buildTime;        //程序构建时间
    QString qtVersion;        // QT版本号
    QString fun_buildTime();  //获取程序构建时间
};

#endif  // SKYSONYA_H

完整的示例地址:https://download.csdn.net/download/skysonya_shisy/89861254

相关推荐
没有梦想的咸鱼185-1037-166318 分钟前
基于R语言机器学习方法在生态经济学领域中的实践技术应用
开发语言·机器学习·数据分析·r语言
向上的车轮43 分钟前
基于go语言的云原生TodoList Demo 项目,验证云原生核心特性
开发语言·云原生·golang
The Chosen One98544 分钟前
C++ : AVL树-详解
开发语言·c++
SNAKEpc121381 小时前
QML和Qt Quick
c++·qt
PH_modest1 小时前
【Qt跬步积累】—— 初识Qt
开发语言·qt
怀旧,1 小时前
【C++】18. 红⿊树实现
开发语言·c++
xiaopengbc2 小时前
在 Python 中实现观察者模式的具体步骤是什么?
开发语言·python·观察者模式
Python大数据分析@2 小时前
python用selenium怎么规避检测?
开发语言·python·selenium·网络爬虫
ThreeAu.2 小时前
Miniconda3搭建Selenium的python虚拟环境全攻略
开发语言·python·selenium·minicoda·python环境配置
zhangfeng11332 小时前
R 语法高亮为什么没有,是需要安装专用的编辑软件,R语言自带的R-gui 功能还是比较简单
开发语言·r语言