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

相关推荐
格林威8 分钟前
Baumer工业相机堡盟工业相机如何通过YoloV8模型实现人物识别(C#)
开发语言·人工智能·数码相机·yolo·计算机视觉·c#
风无雨8 分钟前
前端 cookie 使用
开发语言·前端·javascript
刚入门的大一新生8 分钟前
C++进阶-AVL树(平衡二叉查找树)(难度较高)
开发语言·c++
ggdpzhk18 分钟前
Java :List,LinkedList,ArrayList
java·开发语言·list
三目条件1 小时前
C#将类属性保存到Ini文件方法(利用拓展方法,反射方式获取到分组名和属性名称属性值)
java·开发语言·c#
小堃学编程2 小时前
QT跨平台应用程序开发框架(10)—— Qt窗口
开发语言·qt
轩宇^_^2 小时前
Qt CMake 学习文档
数据库·qt·学习
小徐不徐说2 小时前
QT技巧之快速搭建串口收发平台
开发语言·c++·qt·串口·软件构建·个人开发·通信
天堂陌客2 小时前
QT 交叉编译环境下,嵌入式设备显示字体大小和QT Creator 桌面显示不一致问题解决
开发语言·qt·字库
Q_Q19632884752 小时前
python的抗洪救灾管理系统
开发语言·spring boot·python·django·flask·node.js·php