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.
You can subclass
QMessageBox
and reimplement resize event handler as following: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:
You can edit the css of the label:
You can similarly edit the css of the buttons to add a margin or make them bigger.
For example:
There is also a trick mentioned:
But this doesn't seem to work for everyone.
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:
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.