How do I auto-adjust the size of a QDialog depending on the text length of one of its children?

yan bellavance picture yan bellavance · Nov 4, 2009 · Viewed 26.3k times · Source

I have a QDialog I'm working with. It is made somewhat like a QMessageBox. I noticed that the size of the QMessageBox (and the size of its label) depends on the size of the message displayed.

How would I make the size of my QDialog adjust automatically like a QMessageBox? Presently my QDialog contains a button box and a label, and the QDialog is layout Vertical.

(I know I could just use the message box directly but eventually I will have more complex dialogs.)

Answer

Patrice Bernassola picture Patrice Bernassola · Nov 5, 2009

Automatic solution:

Use layouts and set size policies to QSizePolicy::Expanding. In QtDesigner, once all your children are placed on your QDialog, then click on the Adjust Size button next layout ones. Your QDialog will be automatically resized at runtime.

Manual solution:

The QWidget class has a method adjustSize that resize the QWidget to fit its content. Just call it when all children are set.