说明

在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. delphi 获取网页源代码

    //获取网页源代码 var   s: string; begin   s := WebBrowser1.OleObject.document.body.innerHTML; //body内的所有代码 ...

  2. Android开发 侧边滑动菜单栏SlidingMenu结合Fragment

    SlidingMenu是一个开源项目, https://github.com/jfeinstein10/SlidingMenu .功能是创建侧边滑动菜单栏,效果类似人人Android客户端,可点击按钮 ...

  3. iOS开发——网络编程Swift篇&(三)同步Get方式

    同步Get方式 // MARK: - 同步Get方式 func synchronousGet() { //创建NSURL对象 var url:NSURL! = NSURL(string: " ...

  4. Mysql分表教程

    一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法.目前我所知道的方法都是MYISAM的,INNODB如何做分表并且保留事务和外键,我还不是 ...

  5. java_闭包和回调实现一边按键盘一边演讲

    package ming; interface Teachable { void work(); } class Programer { private String name; public Str ...

  6. boost库在工作(36)网络服务端之六

    在上面介绍了管理所有连接的类,这个类主要就是添加新的连接,或者删除不需要的连接.但是管理的类CAllConnect是没有办法知道什么时候添加,什么时候删除的,它需要从接收到连接类里获取得到新的连接,从 ...

  7. [转]一步步教你如何在 Visual Studio 2013 上使用 Github

    介绍 我承认越是能将事情变简单的工具我越会更多地使用它.尽管我已经知道了足够的命令来使用Github,但我宁愿它被集成到IDE中.在本教程中,我会告诉你使用Visual Studio 2013如何实现 ...

  8. JavaScript 关于this的理解

    this是一个挺神奇的东西,经常不知道它绑定到了那里 ,因此出来了各种绞尽脑汁的面试题. 例1 <script> var person={}; person.name='li'; pers ...

  9. JAVA实现DES加密

    DES加密介绍       DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法.DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少 ...

  10. 物理CPU、物理核跟逻辑核的区分

    一般来说,物理CPU个数×每颗核数就应该等于逻辑CPU的个数,如果不相等的话,则表示服务器的CPU支持超线程技术 ,所以您的电脑是双核的. 一 概念① 物理CPU 实际Server中插槽上的CPU个数 ...