当前位置:网站首页>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
边栏推荐
猜你喜欢
微信小程序-组件传参,状态管理
[typescript] type reduction (including type protection) and type predicate in typescript
Installation steps and environment configuration of vs Code
Wechat applet - screen height
表格与表单相关知识点总结
Basic concepts of MySQL + database system structure + extended application + basic command learning
Qt设置背景图片方法
Clickhouse learning (V) cluster operation
·Let's introduce ourselves to the way of programming·
解决表单校验提示信息不消失问题以及赋值不生效问题
随机推荐
365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
Terminal shell common commands
Introduction to C language array to proficiency (array elaboration)
Win10 搭建MSYS2环境
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
[C language series] - storage of deep anatomical data in memory (II) - floating point type
[JS question solution] questions 1-10 in JS of niuke.com
全局components组件注册
Thrift安装手册
弹性盒子相关知识
uniapp组件之选择选项(如套餐选择)
DAY14:Upload-labs 通关教程
Camunda 1、Camunda工作流-介绍
基础爬虫实战案例之获取游戏商品数据
ClickHouse学习(十)监控运行指标
Clickhouse learning (x) monitoring operation indicators
Day 1
利用Poi-tl在word模板表格单元格内一次插入多张图片和多行单元格相同数据自动合并的功能组件
ClickHouse学习(四)SQL操作
Hcia-r & s self use notes (26) PPP