QT中布局器的addStretch函数使用效果1

本文介绍了QT中的QBoxLayout::addStretch函数,该函数用于在布局器中添加伸缩空间,以调整控件间的间距。通过设置不同的伸缩因子,可以实现控件在布局中的位置调整。举例说明了如何利用addStretch将水平布局器的空白空间按比例分配到各个按钮之间,以达到特定的界面效果。
部署运行你感兴趣的模型镜像

QBoxLayout中addStretch函数说明:

void QBoxLayout::addStretch(int stretch = 0)

Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.

函数的作用是在布局器中增加一个伸缩量,里面的参数表示QSpacerItem的个数,默认值为零,会将你放在layout中的空间压缩成默认的大小。

例如:一个layout布局器,里面有三个控件,一个放在最左边,一个放在最右边,最后一个放在layout的1/3处,这就可以通过addStretch去实现。

例子:用addStretch函数实现将nLayout的布局器的空白空间平均分配

在*.cpp中实现下列代码:

//创建水平布局器
QHBoxLayout *buttonLayout=new QHBoxLayout;
button1=new QPushButton();
button2=new QPushButton();
button3=new QPushButton();
buttonLayout->addStretch(1);  //增加伸缩量
buttonLayout->addWidget(button1);
buttonLayout->addStretch(1);  //增加伸缩量
buttonLayout->addWidget(button2);
buttonLayout->addStretch(1);  //增加伸缩量
buttonLayout->addWidget(button3);
buttonLayout->addStretch(6);  //增加伸缩量
//void QWidget::setContentsMargins(int left, int top, int right, int bottom)
//Sets the margins around the contents of the widget to have the sizes left, top, right, and bottom.
//The margins are used by the layout system, and may be used by subclasses to specify the area to draw in (e.g. excluding the frame).
buttonLayout->setContentsMargins(0,0,0,0);
setLayout(buttonLayout);

程序运行后结果为:

其中四个addStretch()函数用于在button按钮间增加伸缩量,伸缩量的比例为1:1:1:6,意思就是将button以外的空白地方按设定的比例等分为9份并按照设定的顺序放入buttonLayout布局器中。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值