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

相关推荐
nightunderblackcat19 分钟前
新手向:使用Python将多种图像格式统一转换为JPG
开发语言·python
我爱Jack34 分钟前
深入解析 LinkedList
java·开发语言
engchina44 分钟前
Python PDF处理库深度对比:PyMuPDF、pypdfium2、pdfplumber、pdfminer的关系与区别
开发语言·python·pdf
拓端研究室1 小时前
专题:2025供应链数智化与效率提升报告|附100+份报告PDF、原数据表汇总下载
开发语言·php
一百天成为python专家1 小时前
python库之jieba 库
开发语言·人工智能·python·深度学习·机器学习·pycharm·python3.11
Go Dgg2 小时前
【Go + Gin 实现「双 Token」管理员登录】
开发语言·golang·gin
十五年专注C++开发2 小时前
hiredis: 一个轻量级、高性能的 C 语言 Redis 客户端库
开发语言·数据库·c++·redis·缓存
WJ.Polar2 小时前
Python数据容器-集合set
开发语言·python
晓13132 小时前
JavaScript加强篇——第七章 浏览器对象与存储要点
开发语言·javascript·ecmascript
nbsaas-boot2 小时前
Go语言生态成熟度分析:为何Go还无法像Java那样实现注解式框架?
java·开发语言·golang