关于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,你会发现



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


### Qt 布局系统简介 Qt 的布局管理器提供了一种简单而强大的机制来安排窗口部件(widgets),使得它们能够自动调整大小并适应不同的屏幕分辨率和设备方向。通过使用布局,开发者无需手动计算每个控件的位置和尺寸。 #### 创建水平和垂直布局 可以通过 `QHBoxLayout` 和 `QVBoxLayout` 来实现基本的水平和垂直排列。这些类允许将多个子控件按顺序放置在一个容器中,并根据可用空间动态调整其大小[^1]。 ```python from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout app = QApplication([]) window = QWidget() layout = QVBoxLayout() # Create a vertical box layout. button1 = QPushButton("Top Button") button2 = QPushButton("Bottom Button") layout.addWidget(button1) # Add widgets to the layout. layout.addWidget(button2) window.setLayout(layout) # Set the layout on the application's window. window.show() app.exec_() ``` 上述代码展示了如何利用 `QVBoxLayout` 将两个按钮沿竖直方向堆叠起来。 #### 网格布局 (Grid Layouts) 当需要更复杂的布置方案时,比如表格形式的数据展示,则可以考虑采用网格布局 (`QGridLayout`)。这种布局方式允许多个组件被分配到特定行与列之中。 ```python from PyQt5.QtWidgets import QGridLayout, QLabel, QLineEdit grid_layout = QGridLayout() labels = ['Name:', 'Age:', 'Address:'] line_edits = [] for i, label_text in enumerate(labels): label = QLabel(label_text) line_edit = QLineEdit() grid_layout.addWidget(label, i, 0) # Place labels at column 0. grid_layout.addWidget(line_edit, i, 1) # Corresponding input fields placed next to them. line_edits.append(line_edit) # Attach this grid layout into main widget as before... ``` 这里定义了一个简单的表单输入区域,其中每一项都由一个标签加对应的编辑框组成。 #### 自动伸缩策略及其他特性 除了基础的方向控制外,还可以设置各个项目的拉伸因子(stretch factor),从而影响他们在剩余空白区域内所占比例;另外也存在诸如间距(spacing), 边距(margins)之类的属性可供调节以优化整体外观效果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值