- class QGraphicsLinearLayout#
QGraphicsLinearLayout 类提供了一个水平或垂直布局,用于在 Graphics View 中管理小部件。关于更多内容,请参阅更多。
概述#
方法#
def
__init__()def
addItem()def
addStretch()def
alignment()def
dump()def
insertItem()def
insertStretch()def
itemSpacing()定义
removeItem()定义
setSpacing()定义
spacing()
注意
本文档可能包含从C++自动翻译为Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译有误,也可以通过在https:/bugreports.qt.io/projects/PYSIDE上创建工单来告诉我们。
详细描述#
警告
本节包含自动从C++翻译为Python的代码片段,可能包含错误。
线性布局的默认方向为Qt::Horizontal。您可以通过调用
setOrientation(),或将Qt::Vertical传递给QGraphicsLinearLayout构造函数来选择垂直方向。使用
QGraphicsLinearLayout最常见的方法是在堆中创建一个对象,将父小部件传递给构造函数,然后通过调用addItem()添加小部件和布局。scene = QGraphicsScene() textEdit = scene.addWidget(QTextEdit()) pushButton = scene.addWidget(QPushButton()) form = QGraphicsWidget() scene.addItem(form) layout = QGraphicsLinearLayout(form) layout.addItem(textEdit) layout.addItem(pushButton)
如果您在没有将父小部件传递给布局构造函数的情况下操作,需要调用
setLayout()将此布局设置为该小部件的最高级布局,小部件将获取布局的所有权。您可以向线性布局中添加小部件、布局、拉伸(
addStretch(),insertStretch()或者setStretchFactor()),以及间距(setItemSpacing())。布局将获取项的所有权。在某些情况下,当布局项也继承自QGraphicsItem(例如QGraphicsWidget)时,由于布局项属于两种所有权层次结构,所以存在所有权不明确的问题。请参阅setOwnedByLayout()的文档了解如何处理此类情况。您可以通过调用count()和itemAt()来访问布局中的每个项。调用removeAt()或removeItem()将从布局中删除项,而不破坏它。QGraphicsLinearLayout中的大小提示和大小策略#
QGraphicsLinearLayout遵守每个项的大小提示和大小策略。当布局包含比项能够填充的多余空间时,每个项将根据布局对那个项的对齐方式来排列。您可以通过调用setAlignment()为每个项设置对齐方式,并通过调用alignment()检查任何项的对齐方式。默认情况下,项对齐到左上角。QGraphicsLinearLayout内的间距#
项目之间分配一些空间。实际空间量取决于管理小部件的当前样式,但常见间距为4。您也可以通过调用
setSpacing()设置自己的间距,并通过调用spacing()获取当前间距值。如果您想为项目配置单个间距,可以调用setItemSpacing()。QGraphicsLinearLayout中的拉伸因子#
您可以为每个项目分配一个拉伸因子来控制它与其他项目相比将获得多少空间。默认情况下,线性布局中排列的两个相同小部件将具有相同的大小,但如果第一个小部件的拉伸因子为1,第二个小部件的拉伸因子为2,则第一个小部件将获得1/3的可用空间,而第二个将获得2/3。
QGraphicsLinearLayout通过将所有小部件的拉伸因子相加来计算尺寸分布,然后根据可用空间相应地划分。默认情况下,所有小部件的拉伸因子为0;因数为0表示该项目未定义任何拉伸因子;这实际上等同于将拉伸因子设置为1。拉伸因子仅适用于布局的纵向长度方向上的可用空间(根据其方向)。如果您想同时控制项目的水平和垂直拉伸,可以使用QGraphicsGridLayout。QGraphicsLinearLayout与其他布局的比较#
QGraphicsLinearLayout与QVBoxLayout和QHBoxLayout非常相似,但与这些类相反,它用于管理QGraphicsWidget和QGraphicsLayout而不是QWidget和QLayout。- __init__(orientation[, parent=None])#
- 参数:
orientation –
Orientationparent –
QGraphicsLayoutItem
构建一个
QGraphicsLinearLayout实例。您可以传递布局的orientation参数,既可以水平也可以垂直,并将parent传递给QGraphicsLayout构造函数。- __init__([parent=None])
- 参数:
parent –
QGraphicsLayoutItem
使用 Qt::Horizontal 布局方向构建一个
QGraphicsLinearLayout实例。将parent传递给QGraphicsLayout构造函数。- addItem(item)#
- 参数:
item –
QGraphicsLayoutItem
此便利函数等同于调用
insertItem(-1,item).- addStretch([stretch=1])#
- 参数:
stretch – int
此便利函数等同于调用
insertStretch(-1,stretch).- alignment(item)#
- 参数:
item –
QGraphicsLayoutItem- 返回类型:
为
AlignmentFlag的组合
返回
item的对齐方式。默认对齐方式是 Qt::AlignTop | Qt::AlignLeft。对齐方式决定了项目在分配的空间中的位置,在没有足够空间容纳小部件时。
另请参阅
- dump([indent=0])#
- 参数:
indent – int
- insertItem(index, item)#
- 参数:
index – int
item –
QGraphicsLayoutItem
将
item插入布局,位置在index处,或插入到当前位于index处的项目之前。另请参阅
addItem()itemAt()insertStretch()setItemSpacing()- insertStretch(index[, stretch=1])#
- 参数:
index – int
stretch – int
在
index处插入stretch的扩展部分,或插入到当前位于index处的项目之前。- itemSpacing(index)#
- 参数:
index – int
- 返回类型:
float
返回位于
index处的项目后的间隔。另请参阅
返回布局的方向。
另请参阅
- removeItem(item)#
- 参数:
item –
QGraphicsLayoutItem
从布局中移除
item,而不销毁它。项目的所有权转交给调用者。另请参阅
removeAt()insertItem()- setAlignment(item, alignment)#
- 参数:
item –
QGraphicsLayoutItemalignment – 来自
AlignmentFlag
设置
item的对齐方式到alignment。如果item的对齐方式改变,布局将会自动失效。另请参阅
alignment()invalidate()- setItemSpacing(index, spacing)#
- 参数:
index – int
spacing – float
设置
index位置的项目后间距为spacing。另请参阅
- setOrientation(orientation)#
- 参数:
orientation –
Orientation
将布局方向更改到
orientation。更改布局方向将自动使布局失效。另请参阅
- setSpacing(spacing)#
- 参数:
spacing – float
设置布局的间距为
spacing。间距指的是项目之间的垂直和水平距离。- setStretchFactor(item, stretch)#
- 参数:
item –
QGraphicsLayoutItemstretch – int
设置
item的扩展因子为stretch。如果一个项目的扩展因子改变,这个函数将使布局失效。将
stretch设置为 0 将从项目移除扩展因子,这实际上等同于将stretch设置为 1。另请参阅
- spacing()#
- 返回类型:
float
返回布局的间距。间距指项目之间的垂直和水平距离。
另请参阅
- stretchFactor(item)#
- 参数:
item –
QGraphicsLayoutItem- 返回类型:
int
返回
item的伸缩因子。默认伸缩因子为 0,表示项目没有分配的伸缩因子。另请参阅