2

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:

  • QMessageBox::information(...)
  • QMessageBox::critical(...)
  • QMessageBox::question(...)
  • QMessageBox::warning(...)

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.)

cbuchart
  • 10,847
  • 9
  • 53
  • 93
user1911091
  • 1,219
  • 2
  • 14
  • 32

2 Answers2

0

Actually, you can do most of things without creation of your own custom class. QMessageBox provides set of methods that should be useful for you. Here it's example:

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;
    }
}
Max Go
  • 2,092
  • 1
  • 16
  • 26
  • I wannt to add a checkbox "do not show this message again" – user1911091 Sep 29 '14 at 11:19
  • 1
    @user1911091 Could [`QErrorMessage`](http://qt-project.org/doc/qt-5/qerrormessage.html#static-public-members) work for you? – thuga Sep 29 '14 at 11:46
  • No, we are using the 4 messageboxes (warning, question, critical, information) all over our application. Does QErrorMessage style like the operation system, too? – user1911091 Sep 29 '14 at 12:39
0

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"));
cbuchart
  • 10,847
  • 9
  • 53
  • 93