当前位置:网站首页>Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)
Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)
2022-07-29 05:18:00 【cloud_yq】
申明:本文转载自“黄邦勇帅(原名:黄勇)
Qt布局管理(1):部件拉伸(Stretch)原理及大小策略(sizePolicy)
1、部件的大小策略sizePolicy、大小限制、拉伸因子(Stretch Factors)的含义
部件的大小策略、大小限制、拉伸因子从三个方面对布局内的部件怎样进行拉伸以填满布局进行了说明。
拉伸因子描述了各个部件在进行拉伸时,多个部件之间应以怎样的比例进行拉伸,比如把按钮1、按钮2、按钮3的拉伸因子分别为设置为1,2,3,则按钮将按1:2:3的大小进行拉伸以填满整个布局空间(见图5-1)。注意:当主窗口的大小不能按计算出来的比例容纳下所有子部件时,子部件不一定会按设计好的比例进行排列。
大小策略规定了部件以何种方式进行拉伸及压缩,比如部件不能被拉伸或压缩,部件不能被压缩得比大小提示更小等。
部件的大小限制限制了部件可以被拉伸或压缩的范围,比如不能把部件压缩得比最小大小更小,或不能拉伸得比最大大小更大等。
2、QWidget类中对部件大小进行限制的属性
3、设置拉伸因子的函数
4、设置大小策略



5、大小策略与拉伸因子之间的关系
若部件的拉伸因子大于0,则按照拉伸因子的比例分配空间;若拉伸因子为0,则只有在其他部件不需要空间时才会获得空间;也就是说若一些部件拉伸因子大于0,而一些部件拉伸因子为0,则只有拉伸因子大于0的部件会被拉伸,而拉伸因子为0的部件不会被拉伸。
若所有部件的拉伸因子都为0,则按照大小策略的规则对部件进行拉伸。
注意:若部件的大小策略为Fixed,则即使设置了拉伸因子,该部件也不会被拉伸。
以上规则可总结为,拉伸因子会使大小策略不起作用或失效(除了Fixed策略外)
除QSizePolicy::Ignored外,任何部件,都不能压缩得比最小大小更小(若未设置最小大小,则为最小大小提示),任何部件都不能拉伸得比最大大小更大。
示例5.1:大小限制对大小策略的影响
#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");
//为部件设置大小策略
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); //使用布局后,resize函数将不再起作用
pb->setMinimumWidth(11); pb->setMaximumWidth(188); //为pb设置最大/最小大小
pb1->setMinimumWidth(1); //为pb1设置最小大小
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
运行结果及说明见图5-6
示例5.2:部件的优先扩展权
#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"); //该部件具有优先扩展权
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
运行结果及说明见图5-7
示例5.3:拉伸因子与大小策略的关系
#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);
//拉伸因子应位于addWidget()之后,否则拉伸因子将不起作用,设置拉伸因子后扩展优先权将不起作用。
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
运行结果及说明见图5-8
边栏推荐
- Basic use of redis
- rem与px与em异同点
- End of document
- Camunda 1、Camunda工作流-介绍
- Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
- 利用Poi-tl在word模板表格单元格内一次插入多张图片和多行单元格相同数据自动合并的功能组件
- Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
- Do students in the science class really understand the future career planning?
- C language n queen problem
- 全局components组件注册
猜你喜欢

Redirection and files
![[typescript] type reduction (including type protection) and type predicate in typescript](/img/74/52fe769ed3850e01d97cb9fefb7373.png)
[typescript] type reduction (including type protection) and type predicate in typescript

用threejs 技术做游戏跑酷

ClickHouse学习(十)监控运行指标
![[typescript] in depth study of typescript functions](/img/0c/e838960c8efd6e87046d35f8942a07.png)
[typescript] in depth study of typescript functions

AR虚拟增强与现实

ClickHouse学习(十一)clickhouseAPI操作
![[C language series] - a recursive topic](/img/a2/9d65728e672ab16c4fc0d427c241c4.png)
[C language series] - a recursive topic

Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration

关于局部变量
随机推荐
TXT 纯文本操作
ClickHouse学习(七)表查询优化
[typescript] in depth study of typescript functions
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
Detailed explanation of exit interrupt
With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
[C language series] - detailed explanation of file operation (Part 1)
shell基本操作(上)
JS simple code determines whether the device that opens the page is the PC end of the computer, the H5 end of the mobile phone, or the wechat end
Common shortcut keys for Ad
ClickHouse学习(八)物化视图
Terminal shell common commands
[C language series] - storage of deep anatomical data in memory (II) - floating point type
VIM editor use
One dimensional array exercise
Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration
使用微信小程序扫码登录系统PC端web的功能
【TypeScript】TypeScript中类型缩小(含类型保护)与类型谓词
On Paradigm
ClickHouse学习(三)表引擎