26

I have a QMessageBox which I'd like it to be bigger. It's a simple QMessageBox with two standard buttons, Ok and Cancel. The problem is that it is very small for my application's purposes. Code shows like this:

QMessageBox msg;
msg.setText("Whatever");
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msg.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

int ret = msg.exec();
switch (ret) {
  case QMessageBox::Ok:
      ui->textEdit->clear();
      break;
  case QMessageBox::Cancel:
      break;}

I tried several ways to increase the size:

msg.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

msg.setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);

msg.setFixedHeight(600);
msg.setFixedWidth(600);

I even cleared and rebuilt, and it compiles everything but nothing take effect...

Do you have any idea on how to set QMessageBox size "by hand"? Thanks.

DYangu
  • 611
  • 1
  • 7
  • 16

5 Answers5

47

You can edit the css of the label:

msg.setStyleSheet("QLabel{min-width: 700px;}");

You can similarly edit the css of the buttons to add a margin or make them bigger.

For example:

msg.setStyleSheet("QLabel{min-width:500 px; font-size: 24px;} QPushButton{ width:250px; font-size: 18px; }");

There is also a trick mentioned:

QSpacerItem* horizontalSpacer = new QSpacerItem(800, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)msg.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

But this doesn't seem to work for everyone.

coyotte508
  • 9,175
  • 6
  • 44
  • 63
  • 1
    Yes, that's the solution. But now buttons and text display are out of scale. How can I increase both to be proportionate? – DYangu Jun 07 '16 at 01:26
  • It works better with height `msg.setStyleSheet("QLabel{height: 300px; min-height: 300px; max-height: 300px;}");` – DYangu Jun 07 '16 at 02:07
  • 1
    @DYangu edited with another css example added to change the size of the buttons. You can play with the css, but without a precise result in mind I can't give anything more specific. – coyotte508 Jun 07 '16 at 10:51
  • @DYangu see my answer for another solution which might fix this. – Spencer May 27 '18 at 05:50
8

coyotte508's answer caused my layout to be horribly off center and at different widths it was cut off. In searching around further I found this thread which explains a better solution.

In essence the layout of a messagebox is a grid, so you can add a SpacerItem to it to control the width. Here's the c++ code sample from that link:

QMessageBox msgBox;
QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
msgBox.setText( "SomText" );
QGridLayout* layout = (QGridLayout*)msgBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
msgBox.exec();
Spencer
  • 1,931
  • 1
  • 21
  • 44
1

You can subclass QMessageBox and reimplement resize event handler as following:

void MyMessageBox::resizeEvent(QResizeEvent *Event)
{
    QMessageBox::resizeEvent(Event);
    this->setFixedWidth(myFixedWidth);
    this->setFixedHeight(myFixedHeight);
}
ephemerr
  • 1,833
  • 19
  • 22
1

I wanted my QMessageBox width to adapt in proportion to the length of the text content with a certain amount of buffer to avoid line wrap. After surveying numerous forums and threads including this one, I came up with:

int x_offset = (2.0 * MainWindow::geometry().x());
int y_offset = (0.5 * MainWindow::geometry().y());
msgBox.setText(vers_msg.data());
QSpacerItem* horizontalSpacer = new QSpacerItem 
    (8 * vers_msg.size(), 0,
    QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)msgBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(),
    0, 1, layout->columnCount());
msgBox.setGeometry(
    MainWindow::geometry().x() + x_offset,
    MainWindow::geometry().y() + y_offset,
    msgBox.geometry().width(),
    msgBox.geometry().height());

Adjust the hard numbers in x_offset, y_offset and horizontalSpacer to suit your situation. I was hoping it would be easier than this but at least this works.

Dave Sieving
  • 147
  • 2
  • 6
1

Inspired by the "inspect the Qt source code and adapt" approach, this worked for me with Qt 5.12:

if (auto grid = dynamic_cast<QGridLayout*>(msgBox.layout())) {
    if (auto text = grid->itemAtPosition(0, grid->columnCount() - 1); text && text->widget()) {
        text->widget()->setFixedWidth(500);
    }
}

Keep in mind, of course, that this will break if ever they change the way they do layouts of QMessageBox.

sthlm58
  • 1,142
  • 1
  • 9
  • 28