简体   繁体   English

Qt根据布局的宽度设置小部件宽度

[英]qt set widget width according to layout'width

I have three instances of QWidget , WidgetMain and WidgetMerchandise and WidgetCentral . 我有QWidgetWidgetMainWidgetMerchandiseWidgetCentral三个实例。 WidgetMain has BorderLayout , implemented in Border Layout Example . WidgetMain具有BorderLayout ,在Border Layout Example中实现 Now, WidgetMerchandise is on the west side of layout, WidgetCentral is set on Central side of layout.but I would like WidgetMerchandise to have width to 1/4 width of WidgetCentral . 现在, WidgetMerchandise位于布局的西侧, WidgetCentral位于布局的中央。但是我希望WidgetMerchandise宽度为WidgetCentral 1/4宽度。 Do I have to reimplement sizeHint() and minimumSizeHit() or should I achieve this task using qt size policies? 我必须重新实现sizeHint()minimumSizeHit()还是应该使用qt大小策略来完成此任务?

The widget sizes within the same layout are controlled by stretch factors . 相同布局中的小部件大小由拉伸因子控制。 You can call setStretch method of the layout, or your can directly set these factors in the designer mode: 您可以调用布局的setStretch方法,也可以在设计器模式下直接设置以下因素:

在此处输入图片说明

The above picture shows the example of 3 widgets in one layout, and the default layoutStrech is 0,0,0 (it has the same effect as 1,1,1 ). 上图显示了在一个布局中3个小部件的示例,默认的layoutStrech0,0,0 (与1,1,1具有相同的效果)。 You may change it to 1,4,1 for example, so the left most widget will be 1/4 width of the second (central) widget. 例如,您可以将其更改为1,4,1 ,因此最左侧的小部件将是第二个(中央)小部件的1/4宽度。 This is the case when horizontal layout is used, where the stretch factor determines the width. 当使用水平布局时,情况就是这样,其中拉伸因子确定宽度。 Otherwise, if vertical layout is used, the stretch factor represents the ratio of heights. 否则,如果使用垂直布局,则拉伸因子表示高度比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM