How can I resize QMessageBox?

qmessagebox set geometry
pyqt qmessagebox resize
qtwidgets qmessagebox
qmessagebox::no buttons
qmessagebox::critical example
qmessagebox pyqt5
qmessagebox warning pyqt5
qmessagebox change button size

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

c++ - How can I resize QMessageBox?, 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  Qt5 - Resizing a QMessageBox Solution 1: Use setText () with newlines instead of setInformativeText Solution 2: Don't even use QMessageBox. Second-best, IMO, would be to eschew the use Solution 3: Playing games with the QMessageBox's internal QLayout.


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();

How to resize QMessageBox, Hello, My issue is, size of the QMessageBox. I working Windows 7 no problem, I tried the same software with Ubuntu. window size of  Viewable, but not pleasant from a user perspective. If the QMessageBox could just be resized manually as the user desires, it would improve the overall experience greatly. Even with the calls to setSizePolicy () and setSizeGripEnabled () it is still not possible to resize the window.


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);
}

Resizing a QMessageBox?, Resizing a QMessageBox? Hello, I would like to enlarge a QMessageBox but it seems that setMinimumWidth() does not work in this case. I may  If you don't want your QMessageBox to simply widen out with your text, and break a new line at some point, you should create an instance of QMessageBox and set it to have a fixed width (QWidget::setFixedWidth()). Another option is to pass in a text string with new lines ( ) in it, and it can handle it nicely.


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.

Qt5 - Resizing a QMessageBox, Now the QMessageBox implementation itself pretty thoroughly resists attempts to set its size manually, or allow window resizing, or anything  QAbstractButton *QMessageBox:: button ( QMessageBox::StandardButton which) const. Returns a pointer corresponding to the standard button which, or nullptr if the standard button doesn't exist in this message box. This function was introduced in Qt 4.2. See also standardButtons and standardButton ().


Natron/ResizableMessageBox.cpp at master · MrKepzie/Natron , #include <QTextEdit>. NATRON_NAMESPACE_ENTER. // resizable message box,. // see http://www.qtcentre.org/threads/24888-Resizing-a-QMessageBox#  @QtVik. Ok. When a platform (OS) has a FileDialog, Qt uses that one. Called the native Dialog and that is why the setGeometry is ignored. (on linux for instance it can remember the size so it will load some size when shown)


How can I resize QMessageBox? - c++ - android, You can subclass QMessageBox and reimplement resize event handler as following: void MyMessageBox::resizeEvent(QResizeEvent *Event)  Detailed Description. The QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer.. A message box displays a primary text to alert the user to a situation, an informative text to further explain the alert or to ask the user a question, and an optional detailed text to provide even more data if the user requests it.


PyQt5 messagebox, pybutton.resize(200,64) pybutton.move(50, 50) def clickMethod(self): QMessageBox.about(self, "Title", "Message") if __name__ == "__main__": app = QtWidgets  When creating a Python GUI, you may want to show the message box QMessageBox at some point. The QMessageBox is a dialog that shows an informational message. It can optionally ask the user to click any of the buttons that show inside it. Pyqt comes with messagebox support in both PyQt4 and PyQt5. The class to be used is QMessageBox.