说明

在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. aspose.cells 模版

    aspose.cells 模版  http://www.cnblogs.com/whitewolf/archive/2011/03/21/Aspose_Cells_Template1.html

  2. 解决fedora64下vim不能语法着色问题

    初始状态是vim打开任何文件都没有高亮迹象,接不是彩色,也没有下划线,好了,看怎么一步步解决的... 1)#vim ~/.vimrc 竟然没有这个文件,创建之#touch vim ~/.vimrc 添 ...

  3. 使用Areas(区域)分离ASP.NET MVC 项目

    在使用Areas区域时,如果使用默认路由表,将造成路由表冲突,这种情况需要修改一下区域内<区域名称>AreaRegistration.cs和/App_Start/RouteConfig.a ...

  4. debian下Vnc

    1 VNC(Virtual Network Computing,虚拟网络计算)最早是一套由英国剑桥大学AT&T实验 室在2002年开发的轻量型的远程控制计算机软件,其采用了 GPL 授权条款, ...

  5. 小白日记26:kali渗透测试之提权(六)--收集敏感信息,隐藏痕迹

    提权后操作 提权之后,要收集目标系统的重要信息LINUX /etc/resolv.conf       #查看DNS配置,是否可以进行DNS篡改或劫持 /etc/passwd         #存放账 ...

  6. Maven学习小结(六 setting.xml详解[转])

    当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用settings.xml中的settings元素来确定这些配置.这包含了本地仓库位置 ...

  7. X - Urban Elevations

     Urban Elevations  An elevation of a collection of buildings is an orthogonal projection of the buil ...

  8. Android进阶笔记16:ListView篇之ListView刷新显示(全局 和 局部)

    一.ListView内容变化后,动态刷新的步骤(全局刷新): (1)更新适配器Adapter数据源:(不要使用匿名内部类) (2)调用适配器Adapter的刷新方法notifyDataSetChang ...

  9. linux【报错】userdel: user xiaoming is currently used by process 4713解决

    学习linux的初学者肯定会遇到一些莫名其妙的问题,比如我,在学习删除一个用户的时候,就遇到上面的报错 userdel: user xiaoming is currently used by proc ...

  10. Emacs 配置 Python 编程环境

    python编程环境设置涉及到:自动完成.语法检查.虚拟环境. 为了不把系统搞乱,在python的虚拟环境中安装相关的插件. 一.安装python虚拟环境 virtualenvwrapper sudo ...