How do I auto-adjust the size of a QDialog depending on the text length of one of its children?
Solution 1
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.
Solution 2
Set your dialog to be expanding, and very small. Then, be sure to set your message before showing the dialog. When shown, it will try to find its proper size, based on the size of the objects it contains. (This happens recursively, so if the dialog isn't the direct parent of the label in which you show your message, make sure everything between the label and the dialog is set to use layouts.)
yan bellavance
electronics engineer Hardware Description Language native SW language would be C/C++/C# Once you know C++, you know Java so I made a couple of simple android apps. I know its funny but I love VB.net Did a bit of Obj-C for mac but not currenly active Presently using Qt to develop part of a Flight Termination System.
Updated on September 02, 2020Comments
-
yan bellavance over 3 years
I have a
QDialog
I'm working with. It is made somewhat like aQMessageBox
. I noticed that the size of theQMessageBox
(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 aQMessageBox
? Presently myQDialog
contains a button box and a label, and theQDialog
is layout Vertical.(I know I could just use the message box directly but eventually I will have more complex dialogs.)
-
Lars over 10 yearsthe manual solution does not work in my case: i have a QDialog with a QVBoxLayout with a QTableWidget and a QHBoxLayout with 2 buttons, i have set the policies to Expanding and calling adjustSize on the dialog does exactly nothing (visible)
-
Cerno over 6 yearsIt's important to call QApplication::processEvents() before calling adjustSize(), as documented here: daniel-albuschat.blogspot.de/2009/08/…
-
help-info.de over 3 yearsWelcome to Stack Overflow. Before answering an old question having an accepted answer (look for the green ✓) as well as other answers ensure your answer adds something new or is otherwise helpful in relation to them. Here is a guide on How to Answer.
-
Chris P almost 3 yearsAlso use QSpacers