Fix layout size

Is there any way that the layoutStretch property always runs? For instance. I set the value to "1,3,2", but then the widget (label) in the first part ("1" in "1,3,2") expands (when more text is added), and then the ration is 1: 3: 2 more not respected. That is, the ratio "1: 3: 2" turns into something more than "3: 1: 3".

+6
source share
1 answer

You should take a look at the QWidget::sizePolicy . It controls how the layout takes into account the sizeHint() its children when updating the geometry.

So, you need to do the following: Make the layout ignore the horizontal size of the Hints of the child widgets by setting the horizontal size policy of the three child widgets to QSizePolicy::Ignored :

 QLabel *label = ...; ... label->setSizePolicy(QSizePolicy::Ignored, label->sizePolicy().verticalPolicy()); 

(The second argument ensures that this instruction does not change the vertical policy. Of course, you must set the size policy for each child widget, this code example is for label only.)

Note that the contents of your layout should be widgets; It seems to me that size policies cannot be assigned to nested layouts (but I could be wrong). At least using QtDesigner, there is no way to apply the size policy to the layout itself (unless it is a widget layout). See comments for more details.


In QtDesigner, you can set the sizePolicy of child widgets as follows:

Before:
enter image description here
Heat shrinkable:
enter image description here
Select items in the layout:
enter image description here
Set the horizontal size policy to Ignore:
enter image description here
Result:
enter image description here

+7
source

Source: https://habr.com/ru/post/916298/


All Articles