2

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

Pingk
  • 532
  • 2
  • 7
  • 17
  • Did u test `dialog.reSize()` ? – DarkSuniuM Sep 25 '17 at 01:47
  • I don't think neither pyqt5 nor python-3.x makes this not a duplicate of many earlier posts, like [this](https://stackoverflow.com/questions/2655354/how-to-allow-resizing-of-qmessagebox-in-pyqt4), or [this](https://stackoverflow.com/questions/37668820/how-can-i-resize-qmessagebox). – Jason Mar 05 '19 at 15:05

2 Answers2

5

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:

Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
0

In PySide6 this worked for me:

dialog.findChild(QGridLayout).setColumnMinimumWidth(1,len(dialog.informativeText()) * dialog.fontMetrics().averageCharWidth())
Reza
  • 21
  • 3
  • *"Any answer that gets the asker going in the right direction is helpful, but do try to mention any limitations, assumptions or simplifications in your answer. Brevity is acceptable, but fuller explanations are better."* - check [How do I write a good answer](https://stackoverflow.com/help/how-to-answer). – Kuro Neko May 19 '22 at 08:32