Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼。

    QMessageBox box(QMessageBox::Warning,"标题","弹出框按钮更改为中文!");
box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
box.exec ();

弹出效果:

在网上查了很多资料,有各种各样的方法,弄得我晕头转向,毕竟我接触Qt才不过一个月。不过在我坚持不懈的努力下,总算是找到了一个便捷的方法。

    QMessageBox box(QMessageBox::Warning,"标题","弹出框按钮更改为中文!");
box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
box.setButtonText (QMessageBox::Ok,QString("确 定"));
box.setButtonText (QMessageBox::Cancel,QString("取 消"));
box.exec ();

弹出效果:

怎么样,简单吧?

另外再附赠一个简单的弹出框类。虽然还没有办法返回用户点击的按钮,不过已经可以基本做到类似C#的使用效果了。这是我闲的无聊花了整整半天才搞定的。大概高手觉得很无趣吧?不过对我这种初学者来说,能弄到这种程度已经足以让我满足了。

这个类暂时先这样,等以后有机会再做补充。大概……

文件头:

#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H #include <QMessageBox>
#include <QPushButton>
#include <QString>
#include <QMap> enum MessageBoxButtons
{
OK = , //消息框包含“确定”按钮
OKCancel = , //消息框包含“确定”和“取消”按钮
AbortRetryIgnore = , //消息框包含“中止”、“重试”和“忽略”按钮
YesNoCancel = , //消息框包含“是”、“否”和“取消”按钮
YesNo = , //消息框包含“是”和“否”按钮
RetryCancel = //消息框包含“重试”和“取消”按钮
}; enum MessageBoxIcon
{
None = , //消息框未包含符号
Hand = , //该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的
Question = , //该消息框包含一个符号,该符号是由一个圆圈和其中的一个问号组成的
Exclamation = , //该消息框包含一个符号,该符号是由一个黄色背景的三角形及其中的一个感叹号组成的
Asterisk = , //该消息框包含一个符号,该符号是由一个圆圈及其中的小写字母 i 组成的
Stop = , //该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的
Error = , //该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的
Warning = , //该消息框包含一个符号,该符号是由一个黄色背景的三角形及其中的一个感叹号组成的
Information = //该消息框包含一个符号,该符号是由一个圆圈及其中的小写字母 i 组成的
}; class MessageBox
{
public:
static void Show(const QString & content);
static void Show(const QString & content, const QString & caption);
static void Show(const QString & content, const QString & caption, MessageBoxButtons button);
static void Show(const QString & content, const QString & caption, MessageBoxButtons button, MessageBoxIcon icon); private:
static QMap<QString,QMessageBox::StandardButton> GetButton(MessageBoxButtons type);
static QString GetButtonText(QMessageBox::StandardButton standar);
signals: }; #endif // MESSAGEBOX_H

源文件:

#include "MessageBox.h"

QString MessageBox::GetButtonText (QMessageBox::StandardButton standar)
{
switch (standar)
{
case QMessageBox::Ok:
return QString("确 定");
case QMessageBox::Cancel:
return QString("取 消");
case QMessageBox::Abort:
return QString("终 止");
case QMessageBox::Retry:
return QString("重 试");
case QMessageBox::Ignore:
return QString("忽 略");
case QMessageBox::Yes:
return QString("是");
case QMessageBox::No:
return QString("否");
default:
return QString("OK");
}
} QMap<QString,QMessageBox::StandardButton> MessageBox:: GetButton(MessageBoxButtons type)
{
QMap<QString,QMessageBox::StandardButton> map;
switch (type)
{
case MessageBoxButtons():
default:
map.insert (GetButtonText(QMessageBox::Ok),QMessageBox::Ok);
break;
case MessageBoxButtons():
map.insert (GetButtonText(QMessageBox::Ok),QMessageBox::Ok);
map.insert (GetButtonText(QMessageBox::Cancel),QMessageBox::Cancel);
break;
case MessageBoxButtons():
map.insert (GetButtonText(QMessageBox::Abort),QMessageBox::Abort);
map.insert (GetButtonText(QMessageBox::Retry),QMessageBox::Retry);
map.insert (GetButtonText(QMessageBox::Ignore),QMessageBox::Ignore);
break;
case MessageBoxButtons():
map.insert (GetButtonText(QMessageBox::Yes),QMessageBox::Yes);
map.insert (GetButtonText(QMessageBox::No),QMessageBox::No);
map.insert (GetButtonText(QMessageBox::Cancel),QMessageBox::Cancel);
break;
case MessageBoxButtons():
map.insert (GetButtonText(QMessageBox::Yes),QMessageBox::Yes);
map.insert (GetButtonText(QMessageBox::No),QMessageBox::No);
break;
case MessageBoxButtons():
map.insert (GetButtonText(QMessageBox::Retry),QMessageBox::Retry);
map.insert (GetButtonText(QMessageBox::Cancel),QMessageBox::Cancel);
break;
}
return map;
} void MessageBox:: Show(const QString & content, const QString & caption, MessageBoxButtons button,
MessageBoxIcon icon)
{
QMessageBox::Icon con;
switch(icon)
{
case MessageBoxIcon():
con = QMessageBox::NoIcon;
break;
case MessageBoxIcon():
case MessageBoxIcon():
case MessageBoxIcon():
con = QMessageBox::Critical;
break;
case MessageBoxIcon():
con = QMessageBox::Question;
break;
case MessageBoxIcon():
case MessageBoxIcon():
con = QMessageBox::Warning;
break;
default:
con = QMessageBox::Information;
break;
}
QMap<QString,QMessageBox::StandardButton> ms = GetButton(button);
QMessageBox box(con, caption, content);
int size = ms.size ();
switch(size)
{
case :
box.setStandardButtons (ms.first ());
box.setButtonText (ms.values().at (),ms.keys ().at ());
break;
case :
box.setStandardButtons (ms.first () | ms.last ());
box.setButtonText (ms.values().at (),ms.keys ().at ());
box.setButtonText (ms.values().at (),ms.keys ().at ());
break;
case :
box.setStandardButtons (ms.values ().at ()|ms.values ().at ()|ms.values ().at ());
box.setButtonText (ms.values().at (),ms.keys ().at ());
box.setButtonText (ms.values().at (),ms.keys ().at ());
box.setButtonText (ms.values().at (),ms.keys ().at ());
break;
}
box.exec ();
} void MessageBox:: Show(const QString & content, const QString & caption, MessageBoxButtons button)
{
Show(content,caption,button,MessageBoxIcon());
} void MessageBox:: Show(const QString & content, const QString & caption)
{
Show(content,caption,MessageBoxButtons());
} void MessageBox:: Show(const QString & content)
{
Show(content," ");
}

最新文章

  1. display:table-cell
  2. 基于X86平台的PC机通过网络发送一个int(32位)整数的字节顺序
  3. 三道Javascript的练习题
  4. WdatePicker.js开始日期和结束日期比较
  5. 移动BI来袭我们要做哪些准备?
  6. Asp.net attributes collection
  7. SQL Server代理(7/12):作业活动监视器
  8. 转自文翼的博客:将本地时间转换为 GMT 时间
  9. 十六、Swing高级组件
  10. 安装ss
  11. Kali Linux 2016.2初体验使用总结
  12. 打车APP可能的盈利模式
  13. cron表达式详解(Spring定时任务配置时间间隔)
  14. Eclips入门教程
  15. libvirt(virsh命令总结)
  16. WPF 限制Textbox输入的内容
  17. HeadFirst SQL 读书摘要
  18. nyoj 过河问题
  19. windows10中微软小娜cortana如何彻底卸载删除?
  20. 多线程(二)ThreadLocal

热门文章

  1. leetcode -- Merge k Sorted Lists add code
  2. 网络IPC:套接字之建立连接
  3. Android Service与Activity之间通信的几种方式
  4. cocos2d-x之MoonWarriors用c++实现
  5. 【基本计数方法---加法原理和乘法原理】UVa 11538 - Chess Queen
  6. 大表 update 方式
  7. Laravel Eloquent 的条件不等于
  8. CSS实现标题超出宽度省略号来表示
  9. Ajax返回xml类型数据
  10. 【程序员的SQL金典】笔记(第1章~第5章)