Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing the size of layouts

Tags:

qt

qt4

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".

like image 491
David Doria Avatar asked May 21 '12 20:05

David Doria


People also ask

How do I change the layout size in Qt Designer?

Setting A Top Level Layout To check if you have set a top level layout, preview your widget and attempt to resize the window by dragging the size grip. To apply a layout, you can select your choice of layout from the toolbar shown on the left, or from the context menu shown below.

What is layout in Qt?

The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space.

What is QVBoxLayout?

QVBoxLayout organizes your widgets vertically in a window. Instead of organizing all the widgets yourself (specifying the geographic location), you can let PyQt take care of it. Every new widget you add with . addWidget() , is added vertically. Basically you get a vertical list of your widgets.


1 Answers

You should take a look at the property QWidget::sizePolicy. It controls how the layout respects the sizeHint() 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:

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

(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:
enter image description here
Shrinked:
enter image description here
Select the items in the layout:
enter image description here
Set the horizontal size policy to "Ignored":
enter image description here
Result:
enter image description here

like image 100
leemes Avatar answered Nov 05 '22 23:11

leemes