5

I need to customize QMessageBox. I need to remove the frame and title bar and add my own title bar and close button. Also need to replace the standard buttons and probably redo the background color of the box.

Is it possible to subclass it and achieve the above? is there any example anywhere for this? Or, should I just subclass Dialog and create my own message box?

Gerstmann
  • 5,368
  • 6
  • 37
  • 57
go4sri
  • 1,490
  • 2
  • 15
  • 29

2 Answers2

1

This tutorial on custom windows might help you. It's in French but the code examples are in English, it shows how to compose your own title bar, create a window and attach the new title bar on it. I've been through it before, it's pretty straightforward once you've done it.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
0

There is no need to subclass QMessageBox or QDialog. You can pass a QMessageBox the parameter Qt::FramelessWindowHint to remove the frame and buttons. You can also use Qt Style Sheets to style the background of the QMessageBox as well as the buttons. Something like this should work:

msgBox->setStyleSheet("QDialog {background-color: red;}"
                      "QPushButton {background-color: blue;}")

I haven't tested this but it should work or be pretty close.

Anthony
  • 8,570
  • 3
  • 38
  • 46
  • 2
    I need to remove the Window Manager's title bar, but need to have one of my own. I am using [this](http://qt-project.org/faq/answer/how_can_i_handle_events_in_the_titlebar_and_change_its_color_etc) to create my own title bar, but how do I insert it into the message box's layout? – go4sri Sep 14 '12 at 11:44
  • @go4sri You can get the dialog's layout using QWidget::layout(), and then insert the new title bar in the top (i.e., at index 0). I think this will do it: `msgBox->layout()->insertWidget(0, titleBarWidget);`. You might need to static_cast the layout to whatever kind of layout it is (probably a QBoxLayout). You could also create a new QWidget with a QVBoxLayout and use `addWidget()` to add the title bar, then again to add the message box. Not sure if that would work or not. – Anthony Sep 14 '12 at 15:38