问题
I have 3 tables (A,B and C) in a vertical layout.The size policy (Horizontal and vertical) of these 3 are set to expanding. How can I make table-A 3 times the size of B and C and always have that ratio maintained. I am doing this through QT Designer.
Update:
In order to test the layout stretch method. I added four QlistWidgets inside a vertical layout to a form having a horizontal layout. Here is the XML for the form
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>formPracticeClass</class>
<widget class="QMainWindow" name="formPracticeClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>847</width>
<height>661</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>formPractice</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,0,0,0">
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<widget class="QListWidget" name="listWidget_3"/>
</item>
<item>
<widget class="QListWidget" name="listWidget_2"/>
</item>
<item>
<widget class="QListWidget" name="listWidget_4"/>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="formpractice.qrc"/>
</resources>
<connections/>
</ui>
The vertical layout with the 4 widgets has a stretch layout of 1,0,0,0. The height of the topmost widget looks smaller to the others. I want the topmost widget to be 2 times bigger than the others
回答1:
On the layout that contains the 3 tables, set the layoutStretch
property to 3,1,1
in Qt Designer. This specifies that the first element in the layout should have 3 times the space as the other two elements in the layout.
Update:
For your updated example .ui file, you can make the topmost widget to be 2 times larger than the others by setting the layoutStretch
to 2,1,1,1
:
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="2,1,1,1">
Note that the stretch property specifies ratios between the different elements of the layout. In other words, you won't get the desired results if you set one of the elements in the layoutStretch
property to 0
.
来源:https://stackoverflow.com/questions/18215013/layout-how-to-make-one-widget-3x-the-rest-in-a-vertical-layout