说明

在MAC写过QT程序的程序员应该都知道,QT默认的QMessageBox没有MAC系统的效果,在网上找到了一篇关于这方面的文章,但是这篇文章写的有个缺点,就是使用信号的方式,使用起来很不方便。

让你的 Qt 桌面程序看上去更加 native(五):QDialog

解决

我自己写了一个类文件,以达到更好的使用QMessageBox。将下面的代码复制到你的工程中,就可以方便的使用。

头文件#ifndef QMESSAGEBOXEX_H
#define QMESSAGEBOXEX_H #include <QMessageBox> class QMessageBoxEx : public QMessageBox
{
public:
static QMessageBoxEx *shareQMessageBoxEx(); public:
QMessageBoxEx();
int information(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton); int question(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = StandardButtons(Yes | No),
StandardButton defaultButton = NoButton); int warning(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton); int critical(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);
}; #endif // QMESSAGEBOXEX_H
源文件#include "qmessageboxex.h"

static QMessageBoxEx *m_pThis = NULL;
QMessageBoxEx *QMessageBoxEx::shareQMessageBoxEx()
{
if (m_pThis == NULL)
m_pThis = new QMessageBoxEx();
return m_pThis;
} QMessageBoxEx::QMessageBoxEx()
{
} int QMessageBoxEx::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
this->setIcon(QMessageBox::Information);
this->setParent(parent);
this->setWindowTitle(title);
this->setText(text);
this->setStandardButtons(buttons);
this->setDefaultButton(defaultButton);
this->setWindowModality(Qt::WindowModal);
return this->exec();
} int QMessageBoxEx::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
this->setIcon(QMessageBox::Question);
this->setParent(parent);
this->setWindowTitle(title);
this->setText(text);
this->setStandardButtons(buttons);
this->setDefaultButton(defaultButton);
this->setWindowModality(Qt::WindowModal);
return this->exec();
} int QMessageBoxEx::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
this->setIcon(QMessageBox::Warning);
this->setParent(parent);
this->setWindowTitle(title);
this->setText(text);
this->setStandardButtons(buttons);
this->setDefaultButton(defaultButton);
this->setWindowModality(Qt::WindowModal);
return this->exec();
} int QMessageBoxEx::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
this->setIcon(QMessageBox::Critical);
this->setParent(parent);
this->setWindowTitle(title);
this->setText(text);
this->setStandardButtons(buttons);
this->setDefaultButton(defaultButton);
this->setWindowModality(Qt::WindowModal);
return this->exec();
}

使用

调用事例        if (QMessageBox::Yes == QMessageBoxEx::shareQMessageBoxEx()->question(Dialog::shareDialog(),tr("Question"),tr("project folder is exists, will rename \"proj.qt.bak\" !"),
QMessageBox::Yes|QMessageBox::No,QMessageBox::No))
{
if (false == QDir(projectFileInfo.absoluteFilePath()).rename(qtProFileInfo.absoluteFilePath(),"proj.qt.bak"))
{
QMessageBoxEx::shareQMessageBoxEx()->critical(Dialog::shareDialog(),tr("Error"),tr("rename proj.qt failure"));
return;
}
}

预览

QMessageBox 在MAC下更加自然的更多相关文章

  1. 在MAC下搭建JSP开发环境

    1.Mac下JDK的下载安装及配置 在安装jdk之后,需要为jdk安装目录配置环境变量: 任意打开终端,默认是家目录的,然后直接输入: touch .bash_profile 然后输入:vi .bas ...

  2. Mac下改动Android Studio 所用的JDK版本号

    Mac下改动Android Studio 所用的JDK版本号 @author ASCE1885 近期项目从Eclipse+Ant构建模式转移到了Android Studio+Gradle构建模式.自然 ...

  3. MAC下Xcode配置opencv(2017.3.29最新实践,亲测可行)

    本文原创,未经同意,谢绝转载!(转载请告知本人并且经过本人同意--By Pacific-hong) 本人小硕一枚,因为专业方向图像相关,所以用到opencv,然后网上MAC下Xcode配置opencv ...

  4. MAC下Xcode配置opencv(2017.3.29最新实践,亲测可行)(转)

    本文原创,未经同意,谢绝转载!(转载请告知本人并且经过本人同意--By Pacific-hong) 本人小硕一枚,因为专业方向图像相关,所以用到opencv,然后网上MAC下Xcode配置opencv ...

  5. SQL语句:Mac 下 处理myql 不能远程登录和本地登录问题

    mac下,mysql5.7.18连接出错,错误信息为:Access denied for user 'root'@'localhost' (using password: YES) ()里面的为she ...

  6. Mac下安装SQLmap的安装

    1.cd /usr/bin/ 2.sudo git clone https://github.com/sqlmapproject/sqlmap.git sqlmap-dev3.重新打开terminal ...

  7. mac下安装及配置tomcat

    mac下的软件不像windows下的程序那样写注册表,对于tomcat的安装来说,在mac下是名符其实的绿色软件,具体操作如下: 1.到 apache官方主页 下载完整 tar.gz文件包.(没有专门 ...

  8. MAC下 mysql不能插入中文和中文乱码的问题总结

    MAC下 mysql不能插入中文和中文乱码的问题总结 前言 本文中所提到的问题解决方案,都是基于mac环境下的,但其他环境,比如windows应该也适用. 问题描述 本文解决下边两个问题: 往mysq ...

  9. 【开发软件】 在Mac下配置php开发环境:Apache+php+MySql

    本文地址 原文地址   本文提纲: 1. 启动Apache 2. 运行PHP 3. 配置Mysql 4. 使用PHPMyAdmin 5. 附录   有问题请先 看最后的附录   摘要: 系统OS X ...

随机推荐

  1. Communications link failure报错的处理

    一.报错的问题: 测试环境在做压力测试的时候爆出错误 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications l ...

  2. JAVA数组的定义及用法

    数组是有序数据的集合,数组中的每一个元素具有同样的数组名和下标来唯一地确定数组中的元素. 1. 一维数组 1.1 一维数组的定义 type arrayName[]; type[] arrayName; ...

  3. [Angular 2] Understanding @Injectable

    In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...

  4. android151 笔记

    13. 14 .什么是Service以及描述下它的生命周期.Service有哪些启动方法,有什么区别,怎样停用Service? 在Service的生命周期中,被回调的方法比Activity少一些,只有 ...

  5. ptrace x64 转

    #include <sys/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include < ...

  6. Sequence用堆排序

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  7. oracle 排序

    1.ORDER BY 中关于NULL的处理 缺省处理,Oracle在Order by 时认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前. 当然,你也可以使用nulls f ...

  8. LeetCode 238

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  9. 【模拟,时针分针秒针两两夹角】【没有跳坑好兴奋】hdu - 5387 (多校#8 1008)

    算是最好写的一道题了吧,最近模拟没手感,一次过也是很鸡冻o(* ̄▽ ̄*)o 注意事项都在代码里,没有跳坑也不清楚坑点在哪~ #include<cstdio> #include<cst ...

  10. 浅谈 Data URI 与 BASE 64 编码

    前言(废话):鼓捣 Stylish 的时候发现了这么个奇怪的代码行: Data:image/gif;BASE64,R0lGODlhEAAQAKEAAEKF9NPi/AAAAAAAACH5BAEAAAI ...