说明
在MAC写过QT程序的程序员应该都知道,QT默认的QMessageBox没有MAC系统的效果,在网上找到了一篇关于这方面的文章,但是这篇文章写的有个缺点,就是使用信号的方式,使用起来很不方便。
解决
我自己写了一个类文件,以达到更好的使用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; } }