QBoxLayout

Inheritance diagram of PySide2.QtWidgets.QBoxLayout

Inherited by: QHBoxLayout, QVBoxLayout

Synopsis

Functions

Detailed Description

The PySide2.QtWidgets.QBoxLayout class lines up child widgets horizontally or vertically.

PySide2.QtWidgets.QBoxLayout takes the space it gets (from its parent layout or from the PySide2.QtWidgets.QLayout.parentWidget() ), divides it up into a row of boxes, and makes each managed widget fill one box.

../../_images/qhboxlayout-with-5-children.png

If the PySide2.QtWidgets.QBoxLayout ‘s orientation is Qt.Horizontal the boxes are placed in a row, with suitable sizes. Each widget (or other box) will get at least its minimum size and at most its maximum size. Any excess space is shared according to the stretch factors (more about that below).

../../_images/qvboxlayout-with-5-children.png

If the PySide2.QtWidgets.QBoxLayout ‘s orientation is Qt.Vertical , the boxes are placed in a column, again with suitable sizes.

The easiest way to create a PySide2.QtWidgets.QBoxLayout is to use one of the convenience classes, e.g. PySide2.QtWidgets.QHBoxLayout (for Qt.Horizontal boxes) or PySide2.QtWidgets.QVBoxLayout (for Qt.Vertical boxes). You can also use the PySide2.QtWidgets.QBoxLayout constructor directly, specifying its direction as LeftToRight , RightToLeft , TopToBottom , or BottomToTop .

If the PySide2.QtWidgets.QBoxLayout is not the top-level layout (i.e. it is not managing all of the widget’s area and children), you must add it to its parent layout before you can do anything with it. The normal way to add a layout is by calling parentLayout-> PySide2.QtWidgets.QBoxLayout.addLayout() .

Once you have done this, you can add boxes to the PySide2.QtWidgets.QBoxLayout using one of four functions:

Use PySide2.QtWidgets.QBoxLayout.insertWidget() , PySide2.QtWidgets.QBoxLayout.insertSpacing() , PySide2.QtWidgets.QBoxLayout.insertStretch() or PySide2.QtWidgets.QBoxLayout.insertLayout() to insert a box at a specified position in the layout.

PySide2.QtWidgets.QBoxLayout also includes two margin widths:

The margin default is provided by the style. The default margin most Qt styles specify is 9 for child widgets and 11 for windows. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout.

To remove a widget from a layout, call PySide2.QtWidgets.QLayout.removeWidget() . Calling QWidget.hide() on a widget also effectively removes the widget from the layout until QWidget.show() is called.

You will almost always want to use PySide2.QtWidgets.QVBoxLayout and PySide2.QtWidgets.QHBoxLayout rather than PySide2.QtWidgets.QBoxLayout because of their convenient constructors.

class PySide2.QtWidgets.QBoxLayout(arg__1[, parent=nullptr])
Parameters:

Constructs a new PySide2.QtWidgets.QBoxLayout with direction dir and parent widget parent .

PySide2.QtWidgets.QBoxLayout.Direction

This type is used to determine the direction of a box layout.

Constant Description
QBoxLayout.LeftToRight Horizontal from left to right.
QBoxLayout.RightToLeft Horizontal from right to left.
QBoxLayout.TopToBottom Vertical from top to bottom.
QBoxLayout.BottomToTop Vertical from bottom to top.
PySide2.QtWidgets.QBoxLayout.addLayout(layout[, stretch=0])
Parameters:

Adds layout to the end of the box, with serial stretch factor stretch .

PySide2.QtWidgets.QBoxLayout.addSpacerItem(spacerItem)
Parameters:spacerItemPySide2.QtWidgets.QSpacerItem

Adds spacerItem to the end of this box layout.

PySide2.QtWidgets.QBoxLayout.addSpacing(size)
Parameters:sizePySide2.QtCore.int

Adds a non-stretchable space (a PySide2.QtWidgets.QSpacerItem ) with size size to the end of this box layout. PySide2.QtWidgets.QBoxLayout provides default margin and spacing. This function adds additional space.

See also

PySide2.QtWidgets.QBoxLayout.insertSpacing() PySide2.QtWidgets.QBoxLayout.addItem() PySide2.QtWidgets.QSpacerItem

PySide2.QtWidgets.QBoxLayout.addStretch([stretch=0])
Parameters:stretchPySide2.QtCore.int

Adds a stretchable space (a PySide2.QtWidgets.QSpacerItem ) with zero minimum size and stretch factor stretch to the end of this box layout.

See also

PySide2.QtWidgets.QBoxLayout.insertStretch() PySide2.QtWidgets.QBoxLayout.addItem() PySide2.QtWidgets.QSpacerItem

PySide2.QtWidgets.QBoxLayout.addStrut(arg__1)
Parameters:arg__1PySide2.QtCore.int

Limits the perpendicular dimension of the box (e.g. height if the box is LeftToRight ) to a minimum of size . Other constraints may increase the limit.

See also

PySide2.QtWidgets.QBoxLayout.addItem()

PySide2.QtWidgets.QBoxLayout.addWidget(arg__1[, stretch=0[, alignment=Qt.Alignment()]])
Parameters:

Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment .

The stretch factor applies only in the PySide2.QtWidgets.QBoxLayout.direction() of the PySide2.QtWidgets.QBoxLayout , and is relative to the other boxes and widgets in this PySide2.QtWidgets.QBoxLayout . Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the PySide2.QtWidgets.QBoxLayout has a stretch factor greater than zero, the space is distributed according to the PySide2.QtWidgets.QWidget :sizePolicy() of each widget that’s involved.

The alignment is specified by alignment . The default alignment is 0, which means that the widget fills the entire cell.

PySide2.QtWidgets.QBoxLayout.direction()
Return type:PySide2.QtWidgets.QBoxLayout.Direction

Returns the direction of the box. PySide2.QtWidgets.QBoxLayout.addWidget() and PySide2.QtWidgets.QBoxLayout.addSpacing() work in this direction; the stretch stretches in this direction.

PySide2.QtWidgets.QBoxLayout.insertItem(index, arg__2)
Parameters:

Inserts item into this box layout at position index . If index is negative, the item is added at the end.

PySide2.QtWidgets.QBoxLayout.insertLayout(index, layout[, stretch=0])
Parameters:

Inserts layout at position index , with stretch factor stretch . If index is negative, the layout is added at the end.

layout becomes a child of the box layout.

PySide2.QtWidgets.QBoxLayout.insertSpacerItem(index, spacerItem)
Parameters:

Inserts spacerItem at position index , with zero minimum size and stretch factor. If index is negative the space is added at the end.

PySide2.QtWidgets.QBoxLayout.insertSpacing(index, size)
Parameters:
  • indexPySide2.QtCore.int
  • sizePySide2.QtCore.int

Inserts a non-stretchable space (a PySide2.QtWidgets.QSpacerItem ) at position index , with size size . If index is negative the space is added at the end.

The box layout has default margin and spacing. This function adds additional space.

PySide2.QtWidgets.QBoxLayout.insertStretch(index[, stretch=0])
Parameters:
  • indexPySide2.QtCore.int
  • stretchPySide2.QtCore.int

Inserts a stretchable space (a PySide2.QtWidgets.QSpacerItem ) at position index , with zero minimum size and stretch factor stretch . If index is negative the space is added at the end.

PySide2.QtWidgets.QBoxLayout.insertWidget(index, widget[, stretch=0[, alignment=Qt.Alignment()]])
Parameters:
  • indexPySide2.QtCore.int
  • widgetPySide2.QtWidgets.QWidget
  • stretchPySide2.QtCore.int
  • alignmentPySide2.QtCore.Qt.Alignment

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 PySide2.QtWidgets.QBoxLayout.direction() of the PySide2.QtWidgets.QBoxLayout , and is relative to the other boxes and widgets in this PySide2.QtWidgets.QBoxLayout . Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the PySide2.QtWidgets.QBoxLayout has a stretch factor greater than zero, the space is distributed according to the PySide2.QtWidgets.QWidget :sizePolicy() of each widget that’s involved.

The alignment is specified by alignment . The default alignment is 0, which means that the widget fills the entire cell.

PySide2.QtWidgets.QBoxLayout.setDirection(arg__1)
Parameters:arg__1PySide2.QtWidgets.QBoxLayout.Direction

Sets the direction of this layout to direction .

PySide2.QtWidgets.QBoxLayout.setStretch(index, stretch)
Parameters:
  • indexPySide2.QtCore.int
  • stretchPySide2.QtCore.int

Sets the stretch factor at position index . to stretch .

PySide2.QtWidgets.QBoxLayout.setStretchFactor(w, stretch)
Parameters:
Return type:

PySide2.QtCore.bool

Sets the stretch factor for widget to stretch and returns true if widget is found in this layout (not including child layouts); otherwise returns false .

PySide2.QtWidgets.QBoxLayout.setStretchFactor(l, stretch)
Parameters:
Return type:

PySide2.QtCore.bool

This is an overloaded function.

Sets the stretch factor for the layout layout to stretch and returns true if layout is found in this layout (not including child layouts); otherwise returns false .

PySide2.QtWidgets.QBoxLayout.stretch(index)
Parameters:indexPySide2.QtCore.int
Return type:PySide2.QtCore.int

Returns the stretch factor at position index .