关于Qt中的 "stretch factor"


本文参考:

What is the difference between "stretch factor" and "sizePolicy"


主要理解下面红色字体部分:

How do you understand this:

Stretch Factors 
Widgets are normally created without any stretch factor set. When they are laid out in a layout the widgets are given a share of space in accordance with their QWidget::sizePolicy() or their minimum size hint whichever is the greater.
 Stretch factors are used to change how much space widgets are given in proportion to one another. 

void QBoxLayout::insertWidget ( int index, QWidget * widget, int stretch = 0, int alignment = 0 ) 
Inserts widget at position index, with stretch factor stretch and alignment alignment. If index is negative, the widget is added at the end. 
The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more. 
If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved


得到结论:

1  "stretch factor"默认值是  0.

2 如果一个QSplitter控件里面有3个widget,他们的"stretch factor"分别设置为 0,1,2 。

那么 第一个保持它的sizehint()返回的大小。

第三个尽量是第二个的2倍。(只是尽可能的达到这个比例,也可能达不到,因为还有其他的一些限制,比如,控件的minimum)


实践验证:

int main(int argc,char *argv[])
{
    QApplication app(argc, argv);
    QSplitter splitter;
    QTextEdit *t1 = new QTextEdit;
    QTextEdit *t2 = new QTextEdit;
    QTextEdit *t3 = new QTextEdit;
    splitter.addWidget(t1);
    splitter.addWidget(t2);
    splitter.addWidget(t3);
    splitter.setStretchFactor(1,1);
    splitter.setStretchFactor(2,2);
    splitter.show();
    return app.exec();
}


程序启动后:界面如图




可以看到第三个textEdit是第二个的2倍,拉伸一下这个spliter,你会发现



第一个依然不变,第三个还是第二个的二倍。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值