当前位置:网站首页>QT layout management -- Part stretch principle and sizepolicy
QT layout management -- Part stretch principle and sizepolicy
2022-07-29 05:41:00 【cloud_ yq】
Affirming : Reprinted from “ Huang bangyongshuai ( Original name : Huang Yong )
Qt Layout management (1): Part stretch (Stretch) Principle and size strategy (sizePolicy)
1、 Part size strategy sizePolicy、 Size limit 、 Stretching factor (Stretch Factors) The meaning of
Part size strategy 、 Size limit 、 The stretching factor explains how the components in the layout are stretched to fill the layout from three aspects .
The stretch factor describes how each component is stretched , In what proportion should multiple parts be stretched , For example, push the button 1、 Button 2、 Button 3 The stretching factors of are set to 1,2,3, Then the button will press 1:2:3 Stretch to fill the entire layout space ( See the picture 5-1). Be careful : When the size of the main window cannot accommodate all the subassemblies in the calculated proportion , Subassemblies are not necessarily arranged in designed proportions .
The size strategy specifies how the part is stretched and compressed , For example, parts cannot be stretched or compressed , Components cannot be compressed smaller than the size prompt, etc .
The size limit of the part limits the extent to which the part can be stretched or compressed , For example, parts cannot be compressed smaller than the minimum size , Or cannot stretch larger than the maximum size .
2、QWidget Class to limit the size of parts 
3、 Function to set the stretching factor 
4、 Set the size policy 



5、 The relationship between the size of the stretching factor and
If the tensile factor of the part is greater than 0, The space is allocated according to the proportion of the stretching factor ; If the stretching factor is 0, Space is only available when other components do not need space ; That is, if the tensile factor of some parts is greater than 0, Some parts have a tensile factor of 0, Then only the stretching factor is greater than 0 Parts of the are stretched , And the stretching factor is 0 Parts of the are not stretched .
If the tensile factor of all parts is 0, Then stretch the part according to the rules of the size policy .
Be careful : If the size strategy of the part is Fixed, Then even if the stretching factor is set , The component is also not stretched .
The above rules can be summarized as , The stretching factor will make the size strategy ineffective or ineffective ( except Fixed Outside strategy )
except QSizePolicy::Ignored Outside , Any component , Cannot be compressed smaller than the minimum size ( If the minimum size is not set , Is the minimum size prompt ), No part can be stretched larger than the maximum size .
Example 5.1: The impact of size restrictions on size policies
#include<QtWidgets>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){ QApplication a(argc,argv);
QWidget w; QPushButton *pb=new QPushButton("Fixed");
QPushButton *pb1=new QPushButton("MaxSetMin"); QPushButton *pb2=new QPushButton("MaxNoMin");
// Set the size policy for the part
pb->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
pb1->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Fixed);
pb2->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Fixed);
QHBoxLayout *pg=new QHBoxLayout;
pb->resize(222,222); // After using the layout ,resize Functions will no longer work
pb->setMinimumWidth(11); pb->setMaximumWidth(188); // by pb Set the maximum / Minimum size
pb1->setMinimumWidth(1); // by pb1 Set the minimum size
pg->addWidget(pb); pg->addWidget(pb1); pg->addWidget(pb2); w.setLayout(pg);
w.resize(300,200); w.show(); return a.exec(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
The operation results and descriptions are shown in Figure 5-6
Example 5.2: Priority extension of components
#include<QtWidgets>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){ QApplication a(argc,argv);
QWidget w; QPushButton *pb=new QPushButton("Preferred");
QPushButton *pb1=new QPushButton("Expanding"); // This part has priority to expand
pb->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
pb1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
QHBoxLayout *pg=new QHBoxLayout; pg->addWidget(pb); pg->addWidget(pb1);
w.setLayout(pg); w.resize(300,200); w.show(); return a.exec(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
The operation results and descriptions are shown in Figure 5-7
Example 5.3: Relationship between stretching factor and size strategy
#include<QtWidgets>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){ QApplication a(argc,argv);
QWidget w; QPushButton *pb=new QPushButton("Preferred");
QPushButton *pb1=new QPushButton("Expanding"); QPushButton *pb2=new QPushButton("xxx");
QPushButton *pb3=new QPushButton("Ignore");
pb->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
pb1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
pb2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
pb3->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Fixed);
QHBoxLayout *pg=new QHBoxLayout;
pg->addWidget(pb); pg->addWidget(pb1);
pg->addWidget(pb2); pg->addWidget(pb3);
// The stretching factor should be located at addWidget() after , Otherwise, the stretching factor will not work , After setting the stretch factor, the extension priority will not work .
pg->setStretch(0,1); pg->setStretchFactor(pb1,3); pg->setStretch(3,2);
w.setLayout(pg); w.resize(300,100); w.show(); return a.exec(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
The operation results and descriptions are shown in Figure 5-8
边栏推荐
- 记xx公司SQL注入漏洞
- ClickHouse学习(二)ClickHouse单机安装
- 微信小程序更改属性值-setData-双向绑定-model
- Clickhouse learning (x) monitoring operation indicators
- Integer overflow and printing
- [typescript] learn typescript object types in depth
- uniapp组件之tab选项卡滑动切换
- Hcia-r & s self use notes (27) comprehensive experiment
- Relationship between redrawing and reflow
- Wapiti是什么以及使用教程
猜你喜欢

Wapiti是什么以及使用教程
![[C language series] - a recursive topic](/img/a2/9d65728e672ab16c4fc0d427c241c4.png)
[C language series] - a recursive topic
![[C language series] - detailed explanation of file operation (Part 1)](/img/12/2d47fde0385d3f1dcb31f5efa82f7b.png)
[C language series] - detailed explanation of file operation (Part 1)

Day 3

Database operation day 6

Realize simple database query (incomplete)

ClickHouse学习(五)集群操作

Basic concepts of MySQL + database system structure + extended application + basic command learning

微信小程序-组件传参,状态管理

ClickHouse学习(九)clickhouse整合mysql
随机推荐
Clickhouse learning (x) monitoring operation indicators
Qt设置背景图片方法
公众号不支持markdown格式文件编写怎么办?
Using POI TL to insert multiple pictures and the same data of multiple rows of cells into the table cells of word template at one time, it is a functional component for automatic merging
Wechat applet video upload component is directly uploaded to Alibaba cloud OSS
paddle.fluild常量计算报错‘NoneType‘ object has no attribute ‘get_fetch_list‘
[C language series] - string + partial escape character explanation + annotation tips
uniapp组件之倒计时(如阅读协议倒计时、完成学习倒计时)
移动端-flex项目属性
How does the MD editor of CSDN input superscripts and subscripts? The input method of formula and non formula is different
Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration
[C language series] - storage of deep anatomical data in memory (II) - floating point type
Li Kou 994: rotten orange (BFS)
table中同一列中合并相同项
Common shortcut keys for Ad
Wapiti是什么以及使用教程
Realize simple database query (incomplete)
Relationship between link and @import
ClickHouse学习(八)物化视图
Talking about Servlet