PySide6.QtWidgets.QGraphicsLinearLayout¶
- class QGraphicsLinearLayout¶
The
QGraphicsLinearLayout
class provides a horizontal or vertical layout for managing widgets in Graphics View. More…Synopsis¶
Methods¶
def
__init__()
def
addItem()
def
addStretch()
def
alignment()
def
dump()
def
insertItem()
def
insertStretch()
def
itemSpacing()
def
orientation()
def
removeItem()
def
setAlignment()
def
setItemSpacing()
def
setOrientation()
def
setSpacing()
def
spacing()
def
stretchFactor()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
The default orientation for a linear layout is Qt::Horizontal. You can choose a vertical orientation either by calling
setOrientation()
, or by passing Qt::Vertical toQGraphicsLinearLayout
‘s constructor.The most common way to use
QGraphicsLinearLayout
is to construct an object on the heap, passing a parent widget to the constructor, then add widgets and layouts by callingaddItem()
.scene = QGraphicsScene() textEdit = scene.addWidget(QTextEdit()) pushButton = scene.addWidget(QPushButton()) form = QGraphicsWidget() scene.addItem(form) layout = QGraphicsLinearLayout(form) layout.addItem(textEdit) layout.addItem(pushButton)
Alternatively, if you do not pass a parent widget to the layout’s constructor, you will need to call
setLayout()
to set this layout as the top-level layout for that widget, the widget will take ownership of the layout.You can add widgets, layouts, stretches (
addStretch()
,insertStretch()
orsetStretchFactor()
), and spacings (setItemSpacing()
) to a linear layout. The layout takes ownership of the items. In some cases when the layout item also inherits fromQGraphicsItem
(such asQGraphicsWidget
) there will be a ambiguity in ownership because the layout item belongs to two ownership hierarchies. See the documentation ofsetOwnedByLayout()
how to handle this. You can access each item in the layout by callingcount()
anditemAt()
. CallingremoveAt()
orremoveItem()
will remove an item from the layout, without destroying it.Size Hints and Size Policies in QGraphicsLinearLayout¶
QGraphicsLinearLayout
respects each item’s size hints and size policies, and when the layout contains more space than the items can fill, each item is arranged according to the layout’s alignment for that item. You can set an alignment for each item by callingsetAlignment()
, and check the alignment for any item by callingalignment()
. By default, items are aligned to the top left.Spacing within QGraphicsLinearLayout¶
Between the items, the layout distributes some space. The actual amount of space depends on the managed widget’s current style, but the common spacing is 4. You can also set your own spacing by calling
setSpacing()
, and get the current spacing value by callingspacing()
. If you want to configure individual spacing for your items, you can callsetItemSpacing()
.Stretch Factor in QGraphicsLinearLayout¶
You can assign a stretch factor to each item to control how much space it will get compared to the other items. By default, two identical widgets arranged in a linear layout will have the same size, but if the first widget has a stretch factor of 1 and the second widget has a stretch factor of 2, the first widget will get 1/3 of the available space, and the second will get 2/3.
QGraphicsLinearLayout
calculates the distribution of sizes by adding up the stretch factors of all items, and then dividing the available space accordingly. The default stretch factor is 0 for all items; a factor of 0 means the item does not have any defined stretch factor; effectively this is the same as setting the stretch factor to 1. The stretch factor only applies to the available space in the lengthwise direction of the layout (following its orientation). If you want to control both the item’s horizontal and vertical stretch, you can useQGraphicsGridLayout
instead.QGraphicsLinearLayout Compared to Other Layouts¶
QGraphicsLinearLayout
is very similar toQVBoxLayout
andQHBoxLayout
, but in contrast to these classes, it is used to manageQGraphicsWidget
andQGraphicsLayout
instead ofQWidget
andQLayout
.See also
- __init__([parent=None])¶
- Parameters:
parent –
QGraphicsLayoutItem
Constructs a
QGraphicsLinearLayout
instance using Qt::Horizontal orientation.parent
is passed toQGraphicsLayout
‘s constructor.- __init__(orientation[, parent=None])
- Parameters:
orientation –
Orientation
parent –
QGraphicsLayoutItem
Constructs a
QGraphicsLinearLayout
instance. You can pass theorientation
for the layout, either horizontal or vertical, andparent
is passed toQGraphicsLayout
‘s constructor.- addItem(item)¶
- Parameters:
item –
QGraphicsLayoutItem
This convenience function is equivalent to calling
insertItem
(-1,item
).- addStretch([stretch=1])¶
- Parameters:
stretch – int
This convenience function is equivalent to calling
insertStretch
(-1,stretch
).- alignment(item)¶
- Parameters:
item –
QGraphicsLayoutItem
- Return type:
Combination of
AlignmentFlag
Returns the alignment for
item
. The default alignment is Qt::AlignTop | Qt::AlignLeft.The alignment decides how the item is positioned within its assigned space in the case where there’s more space available in the layout than the widgets can occupy.
See also
- dump([indent=0])¶
- Parameters:
indent – int
- insertItem(index, item)¶
- Parameters:
index – int
item –
QGraphicsLayoutItem
Inserts
item
into the layout atindex
, or before any item that is currently atindex
.See also
addItem()
itemAt()
insertStretch()
setItemSpacing()
- insertStretch(index[, stretch=1])¶
- Parameters:
index – int
stretch – int
Inserts a stretch of
stretch
atindex
, or before any item that is currently atindex
.- itemSpacing(index)¶
- Parameters:
index – int
- Return type:
float
Returns the spacing after item at
index
.See also
- orientation()¶
- Return type:
Returns the layout orientation.
See also
- removeItem(item)¶
- Parameters:
item –
QGraphicsLayoutItem
Removes
item
from the layout without destroying it. Ownership ofitem
is transferred to the caller.See also
removeAt()
insertItem()
- setAlignment(item, alignment)¶
- Parameters:
item –
QGraphicsLayoutItem
alignment – Combination of
AlignmentFlag
Sets the alignment of
item
toalignment
. Ifitem
's alignment changes, the layout is automatically invalidated.See also
alignment()
invalidate()
- setItemSpacing(index, spacing)¶
- Parameters:
index – int
spacing – float
Sets the spacing after item at
index
tospacing
.See also
- setOrientation(orientation)¶
- Parameters:
orientation –
Orientation
Change the layout orientation to
orientation
. Changing the layout orientation will automatically invalidate the layout.See also
- setSpacing(spacing)¶
- Parameters:
spacing – float
Sets the layout’s spacing to
spacing
. Spacing refers to the vertical and horizontal distances between items.- setStretchFactor(item, stretch)¶
- Parameters:
item –
QGraphicsLayoutItem
stretch – int
Sets the stretch factor for
item
tostretch
. If an item’s stretch factor changes, this function will invalidate the layout.Setting
stretch
to 0 removes the stretch factor from the item, and is effectively equivalent to settingstretch
to 1.See also
- spacing()¶
- Return type:
float
Returns the layout’s spacing. Spacing refers to the vertical and horizontal distances between items.
See also
- stretchFactor(item)¶
- Parameters:
item –
QGraphicsLayoutItem
- Return type:
int
Returns the stretch factor for
item
. The default stretch factor is 0, meaning that the item has no assigned stretch factor.See also