Is there a way to make the layoutStretch property always be obeyed? E.g. I have it set to “1,3,2”, but then a widget (a label) in the first part (the “1” in “1,3,2”) expands (when more text is added), and then the 1:3:2 ration is no longer respected. That is, the “1:3:2” ratio turns into something more like “3:1:3”.
Share
You should take a look at the property
QWidget::sizePolicy. It controls how the layout respects thesizeHint()of its children when it updates the geometries.So what you need to do is: Make the layout ignore the horizontal sizeHints of the child widgets by setting the horizontal sizePolicy of the three child widgets to
QSizePolicy::Ignored:(The second argument will ensure that the vertical policy isn’t changed by this statement.
Of course, you should set the size policy of every child widget, this example code is only for the label.)
Note that the contents of your layout have to be widgets; I think nested layouts can’t be assigned a size policy (but I might be wrong). At least using QtDesigner, there is no way of applying a size policy to a layout itself (if it isn’t the layout of a widget). See comments for details.
In QtDesigner, you can set the sizePolicy of the child widgets like this:
Before:





Shrinked:
Select the items in the layout:
Set the horizontal size policy to “Ignored”:
Result: