Pingk
Pingk

Reputation: 542

How to resize QMessageBox in Python 3.5, PyQt5

I have a QMessageBox that is too short.

I've tried various methods of increasing the height, such as resize, setMinimumHeight, setSizeIncrement + setSizeGripEnabled, but none of these attempts work. Setting SizeGrip doesn't allow me to resize the window at all.

dialog = QMessageBox(self)

dialog.setWindowTitle('About this programme')
dialog.setText('Introductory text')
dialog.setDetailedText('A bunch more text')

dialog.setMinimumHeight(500)
dialog.setSizeIncrement(1, 1)
dialog.setSizeGripEnabled(True)

dialog.show()

For reference in the picture, the window labelled Rotascript has size 360x, 370y.

What am I doing wrong, or is this some kind of bug?

Thanks in advance.

Screen is a 27" 1080p display

Upvotes: 3

Views: 10635

Answers (2)

Reza
Reza

Reputation: 41

In PySide6 this worked for me:

dialog.findChild(QGridLayout).setColumnMinimumWidth(1,len(dialog.informativeText()) * dialog.fontMetrics().averageCharWidth())

Upvotes: 1

Lorem Ipsum
Lorem Ipsum

Reputation: 4554

You're not doing anything wrong. The QMessageBox is notoriously difficult to resize.

Generally, people use one of three approaches:

  1. Use setText() with newlines instead of setInformativeText()

    The message box will resize with respect to the text set by the setText() method. It's hacky, but you could simply include a bunch of white space in setText().

  2. Try overriding the layout of QMessageBox

    This is includes fiddling with setFixedSize or overriding the widget's event method. Here is a good example from a related SO post.

  3. Reimplement QMessageBox

    A fundamental property of QMessageBox is that it doesn't resize. So, through backwards logic, if you want a message box which resizes, you actually don't want a message box. However, the QMessageBox inherits from QDialog. You might be able to achieve what you want by going up a level in the inheritance chain and re-implementing the features you want from a QDialog.

The following resources informed my response:

Upvotes: 6

Related Questions