PySide6.QtWidgets.QLayout¶
- class QLayout¶
The
QLayoutclass is the base class of geometry managers.Details
This is an abstract base class inherited by the concrete classes
QBoxLayout,QGridLayout,QFormLayout, andQStackedLayout.For users of
QLayoutsubclasses or ofQMainWindowthere is seldom any need to use the basic functions provided byQLayout, such assetSizeConstraint()orsetMenuBar(). See Layout Management for more information.To make your own layout manager, implement the functions
addItem(),sizeHint(),setGeometry(),itemAt()andtakeAt(). You should also implementminimumSize()to ensure your layout isn’t resized to zero size if there is too little space. To support children whose heights depend on their widths, implementhasHeightForWidth()andheightForWidth(). See the Flow Layout example for more information about implementing custom layout managers.Geometry management stops when the layout manager is deleted.
See also
QLayoutItemLayout Management Basic Layouts Example Flow Layout ExampleInherited by:
QStackedLayout,QGridLayout,QFormLayout,QBoxLayout,QVBoxLayout,QHBoxLayoutSynopsis¶
Properties¶
horizontalSizeConstraintᅟ- Horizontal size constraintsizeConstraintᅟ- The resize mode of the layout. Setting the size constraint for the dialog. Setting a vertical or horizontal size constraint will override thisspacingᅟ- The spacing between widgets inside the layoutverticalSizeConstraintᅟ- Vertical size constraint
Methods¶
def
__init__()def
activate()def
addChildLayout()def
addChildWidget()def
addWidget()def
adoptLayout()def
alignmentRect()def
contentsRect()def
isEnabled()def
menuBar()def
parentWidget()def
removeItem()def
removeWidget()def
setAlignment()def
setEnabled()def
setMenuBar()def
sizeConstraint()def
totalSizeHint()def
update()def
widgetEvent()
Virtual methods¶
def
addItem()def
count()def
indexOf()def
itemAt()def
replaceWidget()def
setSpacing()def
spacing()def
takeAt()
Static functions¶
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
- class SizeConstraint¶
Describes how the layout constrains the size of the widget.
A vertical constraint affects the widget’s height, while a horizontal constraint affects its width.
The possible values are:
Constant
Description
QLayout.SizeConstraint.SetDefaultConstraint
In the constrained orientation(s), the widget’s minimum extent is set to
minimumSize(), unless a minimum size has already been set.QLayout.SizeConstraint.SetFixedSize
In the constrained orientation(s), the widget’s extent is set to
sizeHint(), and it cannot be resized in that direction.QLayout.SizeConstraint.SetMinimumSize
In the constrained orientation(s), the widget’s minimum extent is set to
minimumSize().QLayout.SizeConstraint.SetMaximumSize
In the constrained orientation(s), the widget’s maximum extent is set to
maximumSize().QLayout.SizeConstraint.SetMinAndMaxSize
In the constrained orientation(s), the widget’s minimum extent is set to
minimumSize(), and the maximum extent is set tomaximumSize().QLayout.SizeConstraint.SetNoConstraint
No size constraints are applied to the widget.
Note
Properties can be used directly when
from __feature__ import true_propertyis used or via accessor functions otherwise.- Access functions:
- property horizontalSizeConstraintᅟ: QLayout.SizeConstraint¶
This property holds The horizontal size constraint..
The default mode is
SetDefaultConstraintSee also
- Access functions:
- property sizeConstraintᅟ: QLayout.SizeConstraint¶
This property holds the resize mode of the layout. Setting the size constraint for the dialog. Setting a vertical or horizontal size constraint will override this..
The default mode is
SetDefaultConstraint.- Access functions:
- property spacingᅟ: int¶
This property holds the spacing between widgets inside the layout.
If no value is explicitly set, the layout’s spacing is inherited from the parent layout, or from the style settings for the parent widget.
For
QGridLayoutandQFormLayout, it is possible to set different horizontal and vertical spacings usingsetHorizontalSpacing()andsetVerticalSpacing(). In that case, spacing() returns -1.- Access functions:
- property verticalSizeConstraintᅟ: QLayout.SizeConstraint¶
This property holds The vertical size constraint..
The default mode is
SetDefaultConstraint- Access functions:
Constructs a new top-level
QLayout, with parentparent.The layout is set directly as the top-level layout for
parent. There can be only one top-level layout for a widget. It is returned bylayout().If
parentisNone, then you must insert this layout into another layout, or set it as a widget’s layout usingsetLayout().See also
- activate()¶
- Return type:
bool
Redoes the layout for
parentWidget()if necessary.You should generally not need to call this because it is automatically called at the most appropriate times. It returns true if the layout was redone.
See also
This function is called from
addLayout()orinsertLayout()functions in subclasses to add layoutchildLayoutas a sub-layout.The only scenario in which you need to call it directly is if you implement a custom layout that supports nested layouts.
See also
This function is called from
addWidget()functions in subclasses to addwas a managed widget of a layout.If
wis already managed by a layout, this function will produce a warning, and removewfrom that layout. This function must therefore be called before addingwto the layout’s data structure.- abstract addItem(item)¶
- Parameters:
item –
QLayoutItem
Implemented in subclasses to add an
item. How it is added is specific to each subclass.This function is not usually called in application code. To add a widget to a layout, use the
addWidget()function; to add a child layout, use the addLayout() function provided by the relevantQLayoutsubclass.Note
The ownership of
itemis transferred to the layout, and it’s the layout’s responsibility to delete it.See also
Adds widget
wto this layout in a manner specific to the layout. This function usesaddItem().Returns the rectangle that should be covered when the geometry of this layout is set to
r, provided that this layout supportssetAlignment().The result is derived from
sizeHint()andexpandingDirections(). It is never larger thanr.Returns a size that satisfies all size constraints on
widget, includingheightForWidth()and that is as close as possible tosize.Returns the margins used around the layout.
By default,
QLayoutuses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.See also
Getter of property
contentsMarginsᅟ.Returns the layout’s
geometry()rectangle, but taking into account the contents margins.See also
- abstract count()¶
- Return type:
int
Must be implemented in subclasses to return the number of items in the layout.
See also
- getContentsMargins()¶
- Return type:
PyObject
For each of
left,top,rightandbottomthat is notNone, stores the size of the margin named in the location the pointer refers to.By default,
QLayoutuses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.- horizontalSizeConstraint()¶
- Return type:
See also
Getter of property
horizontalSizeConstraintᅟ.- indexOf(layoutItem)¶
- Parameters:
layoutItem –
QLayoutItem- Return type:
int
Searches for layout item
layoutItemin this layout (not including child layouts).Returns the index of
layoutItem, or -1 iflayoutItemis not found.- indexOf(widget)
- Parameters:
widget –
QWidget- Return type:
int
Searches for widget
widgetin this layout (not including child layouts).Returns the index of
widget, or -1 ifwidgetis not found.The default implementation iterates over all items using
itemAt().- isEnabled()¶
- Return type:
bool
Returns
trueif the layout is enabled; otherwise returnsfalse.See also
- abstract itemAt(index)¶
- Parameters:
index – int
- Return type:
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Must be implemented in subclasses to return the layout item at
index. If there is no such item, the function must returnNone. Items are numbered consecutively from 0. If an item is deleted, other items will be renumbered.This function can be used to iterate over a layout. The following code will draw a rectangle for each layout item in the layout structure of the widget.
- Return type:
Returns the menu bar set for this layout, or
Noneif no menu bar is set.See also
Returns the parent widget of this layout, or
Noneif this layout is not installed on any widget.If the layout is a sub-layout, this function returns the parent widget of the parent layout.
See also
parent()- removeItem(item)¶
- Parameters:
item –
QLayoutItem
Removes the layout item
itemfrom the layout. It is the caller’s responsibility to delete the item.Notice that
itemcan be a layout (sinceQLayoutinheritsQLayoutItem).See also
Removes the widget
widgetfrom the layout. After this call, it is the caller’s responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary.Note
The ownership of
widgetremains the same as when it was added.See also
- replaceWidget(from, to[, options=Qt.FindChildrenRecursively])¶
- Parameters:
from –
QWidgetto –
QWidgetoptions – Combination of
FindChildOption
- Return type:
Searches for widget
fromand replaces it with widgettoif found. Returns the layout item that contains the widgetfromon success. OtherwiseNoneis returned. IfoptionscontainsQt::FindChildrenRecursively(the default), sub-layouts are searched for doing the replacement. Any other flag inoptionsis ignored.Notice that the returned item therefore might not belong to this layout, but to a sub-layout.
The returned layout item is no longer owned by the layout and should be either deleted or inserted to another layout. The widget
fromis no longer managed by the layout and may need to be deleted or hidden. The parent of widgetfromis left unchanged.This function works for the built-in Qt layouts, but might not work for custom layouts.
See also
- setAlignment(l, alignment)¶
- Parameters:
l –
QLayoutalignment – Combination of
AlignmentFlag
- Return type:
bool
Sets the alignment for the layout
ltoalignmentand returnstrueiflis found in this layout (not including child layouts); otherwise returnsfalse.- setAlignment(w, alignment)
- Parameters:
w –
QWidgetalignment – Combination of
AlignmentFlag
- Return type:
bool
Sets the alignment for widget
wtoalignmentand returns true ifwis found in this layout (not including child layouts); otherwise returnsfalse.Sets the
marginsto use around the layout.By default,
QLayoutuses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.See also
Setter of property
contentsMarginsᅟ.- setContentsMargins(left, top, right, bottom)
- Parameters:
left – int
top – int
right – int
bottom – int
Sets the
left,top,right, andbottommargins to use around the layout.By default,
QLayoutuses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.- setEnabled(enable)¶
- Parameters:
enable – bool
Enables this layout if
enableis true, otherwise disables it.An enabled layout adjusts dynamically to changes; a disabled layout acts as if it did not exist.
By default all layouts are enabled.
See also
- setHorizontalSizeConstraint(constraint)¶
- Parameters:
constraint –
SizeConstraint
See also
Setter of property
horizontalSizeConstraintᅟ.Tells the geometry manager to place the menu bar
widgetat the top ofparentWidget(), outsidecontentsMargins(). All child widgets are placed below the bottom edge of the menu bar.See also
- setSizeConstraint(constraint)¶
- Parameters:
constraint –
SizeConstraint
See also
Setter of property
sizeConstraintᅟ.- setSizeConstraints(horizontal, vertical)¶
- Parameters:
horizontal –
SizeConstraintvertical –
SizeConstraint
Sets both the
horizontalandverticalsize constraints.Setter of property
spacingᅟ.- setVerticalSizeConstraint(constraint)¶
- Parameters:
constraint –
SizeConstraint
See also
Setter of property
verticalSizeConstraintᅟ.- sizeConstraint()¶
- Return type:
See also
Getter of property
sizeConstraintᅟ.- spacing()¶
- Return type:
int
See also
Getter of property
spacingᅟ.- abstract takeAt(index)¶
- Parameters:
index – int
- Return type:
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Must be implemented in subclasses to remove the layout item at
indexfrom the layout, and return the item. If there is no such item, the function must do nothing and return 0. Items are numbered consecutively from 0. If an item is removed, other items will be renumbered.The following code fragment shows a safe way to remove all items from a layout:
- totalHeightForWidth(w)¶
- Parameters:
w – int
- Return type:
int
- totalMinimumHeightForWidth(w)¶
- Parameters:
w – int
- Return type:
int
- unsetContentsMargins()¶
Unsets any user-defined margins around the layout. The layout will use the default values provided by the style.
See also
Reset function of property
contentsMarginsᅟ.- update()¶
Updates the layout for
parentWidget().You should generally not need to call this because it is automatically called at the most appropriate times.
See also
activate()invalidate()- verticalSizeConstraint()¶
- Return type:
See also
Getter of property
verticalSizeConstraintᅟ.