qtqmessageboxqstyle

Where does QMessageBox get its styleguide, font-size, ... from?


I implemented a custom QMessageBox, inherited from QDialog. (Using qt 4.8.6)

The problem is now that all the custom messageboxes look totally different from the QMessageBox static functions:

They differ in size, font, fontsize, icons, background (the static qmessageboxs have two background colors), etc, ... .

The only thing i found was how to access the operation system specific messagebox icons.

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

Is there something similar for the font or the whole style.

I know QMessagebox uses operation system specific styleguides. But i can not find them. You can view the source here.

So my question is how to make a custom QMessageBox, inherited from QDialog look like the static QMessageBox::... functions?

(If i could access the QMessageBox object, created in this static function calls i could read out all the style and font parameters. But this is not possible.)


Solution

  • A bit late but today I faced a similar problem, not related to adding new elements but to changing some of them. My solution: to use QProxyStyle (Qt 5+). It basically allows you to re-implement only certain aspects of a base style without re-implementing it completely. Specially helpful if you use styles created by QStyleFactory.

    Here an example of overriding the default icon on 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);
      }
    };
    

    Then set the style to your app:

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