2014-09-29 69 views
2

我实现了一个从QDialog继承的定制QMessageBox。 (使用Qt 4.8.6)QMessageBox从何处获取styleguide,font-size,...?

的问题是,现在所有的自定义消息框看起来完全不同于QMessageBox提示静态函数:

  • QMessageBox提示::信息(...)
  • QMessageBox提示: :关键(...)
  • QMessageBox提示::问题(...)
  • QMessageBox提示::警告(...)

它们在大小,字体,字体大小,图标,背景(静态qmessageboxs有两种背景颜色)等方面不同,...。

我唯一发现的是如何访问操作系统特定的消息框图标。

QStyle *style = QApplication::style(); 
QIcon tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation, 0, this);//for QMessageBox::Information 

是否有类似的字体或整体风格。

我知道QMessagebox使用操作系统特定的风格指南。但我找不到它们。 您可以查看源代码here

所以我的问题是如何使自定义QMessageBox从QDialog继承看起来像静态QMessageBox :: ...函数?

其实(如果我能访问QMessageBox提示对象,在这种静态函数调用,我可以读出所有的样式和字体参数创建的。但是,这是不可能的。)

回答

0

有点晚了,但今天我遇到了类似的问题,不涉及增加新的元素,但改变其中的一些。我的解决方案:使用QProxyStyle(Qt 5+)。它基本上允许您仅重新实现基本样式的某些方面而无需完全重新实现它。如果您使用由QStyleFactory创建的样式,特别有用。

这里是覆盖QMessageBox::information上的默认图标的示例。

class MyProxyStyle : public QProxyStyle { 
public: 
    MyProxyStyle(const QString& name) : 
    QProxyStyle(name) {} 

    virtual QIcon standardIcon(StandardPixmap standardIcon, 
          const QStyleOption *option, 
          const QWidget *widget) const override { 
    if (standardIcon == SP_MessageBoxInformation) 
     return QIcon(":/my_mb_info.ico"); 
    return QProxyStyle::standardIcon(standardIcon, option, widget); 
    } 
}; 

然后样式设置为你的应用程序:

qApp->setStyle(new MyProxyStyle("Fusion")); 
0

,你可以做的事情它不创建您自己的自定义类。 QMessageBox提供了一组应该对您有用的方法。这是例如:

QMessageBox msgBox; 
msgBox.setText(text); 
msgBox.setWindowTitle(title); 
msgBox.setIcon(icon); 

msgBox.setStandardButtons(standardButtons); 
QList<QAbstractButton*> buttons = msgBox.buttons(); 
foreach(QAbstractButton* btn, buttons) 
{ 
    QMessageBox::ButtonRole role = msgBox.buttonRole(btn); 
    switch(role) 
    { 
     case QMessageBox::YesRole: 
      btn->setShortcut(QKeySequence("y")); 
     break; 
     case QMessageBox::NoRole: 
      btn->setShortcut(QKeySequence("n")); 
     break; 
    } 
} 
+0

我wannt添加勾选 “不再显示此消息” – user1911091 2014-09-29 11:19:54

+1

@ user1911091莫非['QErrorMessage'(HTTP:// QT -project.org/doc/qt-5/qerrormessage.html#static-public-members)为你工作? – thuga 2014-09-29 11:46:43

+0

不,我们在整个应用程序中使用4个消息框(警告,问题,关键信息)。 也有类似操作系统的QErrorMessage风格吗? – user1911091 2014-09-29 12:39:13