当前位置: 首页 > article >正文

QT实现QMessageBox中文按钮

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

实现代码:

//中文提示对话框
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;
}

具体使用:

//文件
void MainWindow::slot_file_triggered()
{

    //输入对话框
    QString strInfo = skysonya.inputDialog("对话框", "请输入:");
    skysonya.messageBox("warning", "输入对话框", strInfo);
}
自定义类:
#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


http://www.kler.cn/news/340425.html

相关文章:

  • C语言基础题(力扣):最低加油次数
  • Navicat for MySQL 常见问题
  • 【重学 MySQL】四十八、DCL 中的 commit 和 rollback
  • 【Linux报错】“-bash: cd: too many arguments“
  • Python | Leetcode Python题解之第457题环形数组是否存在循环
  • C++竞赛初阶—— 石头剪子布
  • Ubuntu 上安装 MySQL 并且实现远程登录
  • 李沐 X动手学深度学习 数据操作+数据预处理 学习笔记(无代码,纯理论部分)
  • 测试-BUG篇
  • 宏队列和微队列
  • YOLO11模型推理 | 目标检测与跟踪 | 实例分割 | 关键点估计 | OBB旋转目标检测
  • 计算机网络:数据链路层 —— 数据链路层概述
  • java脚手架系列1--模块化、多环境
  • 百度文心智能体平台开发萌猫科研加油喵
  • FFMpeg源码分析,关键结构体分析(一)
  • 20241008深度学习动手篇
  • 如何避免 JavaScript 模块化中的函数未定义陷阱
  • 使用正则表达式删除文本的奇数行或者偶数行
  • k8s之ingress-nginx-controller安装
  • 04. prometheus 监控 Windows 服务器