内容表

上一话题

QLabel

下一话题

QLayoutItem

QLayout

QLayout class is the base class of geometry managers. 更多

Inheritance diagram of PySide2.QtWidgets.QLayout

继承者: QBoxLayout , QFormLayout , QGridLayout , QHBoxLayout , QStackedLayout , QVBoxLayout

概要

函数

虚函数

静态函数

详细描述

这是被继承的抽象基类,由具体类 QBoxLayout , QGridLayout , QFormLayout ,和 QStackedLayout .

For users of QLayout subclasses or of QMainWindow there is seldom any need to use the basic functions provided by QLayout ,譬如 setSizeConstraint() or setMenuBar() 。见 布局管理 了解更多信息。

To make your own layout manager, implement the functions addItem() , sizeHint() , setGeometry() , itemAt() and takeAt() . You should also implement minimumSize() 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, implement hasHeightForWidth() and heightForWidth() 。见 Border Layout and Flow Layout examples for more information about implementing custom layout managers.

几何体管理停止,当布局管理器被删除时。

class QLayout

QLayout(parent)

param parent

QWidget

Constructs a new child QLayout .

This layout has to be inserted into another layout before geometry management will work.

Constructs a new top-level QLayout ,采用父级 parent . parent may not be None .

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 by layout() .

PySide2.QtWidgets.QLayout. SizeConstraint

可能的值:

常量

描述

QLayout.SetDefaultConstraint

The main widget’s minimum size is set to minimumSize() , unless the widget already has a minimum size.

QLayout.SetFixedSize

The main widget’s size is set to sizeHint() ; it cannot be resized at all.

QLayout.SetMinimumSize

The main widget’s minimum size is set to minimumSize() ; it cannot be smaller.

QLayout.SetMaximumSize

The main widget’s maximum size is set to maximumSize() ; it cannot be larger.

QLayout.SetMinAndMaxSize

The main widget’s minimum size is set to minimumSize() and its maximum size is set to maximumSize() .

QLayout.SetNoConstraint

Widget 不受约束。

PySide2.QtWidgets.QLayout. activate ( )
返回类型

bool

重做布局为 parentWidget() 若有必要。

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.

PySide2.QtWidgets.QLayout. addChildLayout ( l )
参数

l QLayout

此函数被调用从 addLayout() or insertLayout() functions in subclasses to add layout l as 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.

PySide2.QtWidgets.QLayout. addChildWidget ( w )
参数

w QWidget

此函数被调用从 addWidget() functions in subclasses to add w as a managed widget of a layout.

w is already managed by a layout, this function will give a warning and remove w from that layout. This function must therefore be called before adding w to the layout’s data structure.

PySide2.QtWidgets.QLayout. addItem ( arg__1 )
参数

arg__1 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 relevant QLayout 子类。

注意

The ownership of item is transferred to the layout, and it’s the layout’s responsibility to delete it.

PySide2.QtWidgets.QLayout. addWidget ( w )
参数

w QWidget

添加 Widget w to this layout in a manner specific to the layout. This function uses addItem() .

PySide2.QtWidgets.QLayout. adoptLayout ( layout )
参数

layout QLayout

返回类型

bool

PySide2.QtWidgets.QLayout. alignmentRect ( arg__1 )
参数

arg__1 QRect

返回类型

QRect

Returns the rectangle that should be covered when the geometry of this layout is set to r , provided that this layout supports setAlignment() .

The result is derived from sizeHint() and expanding(). It is never larger than r .

static PySide2.QtWidgets.QLayout. closestAcceptableSize ( w , s )
参数
返回类型

QSize

Returns a size that satisfies all size constraints on widget ,包括 heightForWidth() and that is as close as possible to size .

PySide2.QtWidgets.QLayout. contentsMargins ( )
返回类型

QMargins

返回围绕布局所使用的边距。

默认情况下, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

PySide2.QtWidgets.QLayout. contentsRect ( )
返回类型

QRect

Returns the layout’s geometry() rectangle, but taking into account the contents margins.

PySide2.QtWidgets.QLayout. count ( )
返回类型

int

Must be implemented in subclasses to return the number of items in the layout.

另请参阅

itemAt()

PySide2.QtWidgets.QLayout. getContentsMargins ( )

For each of left , top , right and bottom that is not None , stores the size of the margin named in the location the pointer refers to.

默认情况下, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

另请参阅

setContentsMargins() pixelMetric() PM_LayoutLeftMargin PM_LayoutTopMargin PM_LayoutRightMargin PM_LayoutBottomMargin

PySide2.QtWidgets.QLayout. indexOf ( arg__1 )
参数

arg__1 QLayoutItem

返回类型

int

搜索布局项 layoutItem 在此布局 (不包括子级布局)。

Returns the index of layoutItem , or -1 if layoutItem 找不到。

PySide2.QtWidgets.QLayout. indexOf ( arg__1 )
参数

arg__1 QWidget

返回类型

int

搜索小部件 widget 在此布局 (不包括子级布局)。

Returns the index of widget , or -1 if widget 找不到。

默认实现遍历所有项,使用 itemAt()

PySide2.QtWidgets.QLayout. isEnabled ( )
返回类型

bool

返回 true 若布局被启用;否则返回 false .

另请参阅

setEnabled()

PySide2.QtWidgets.QLayout. itemAt ( index )
参数

index int

返回类型

QLayoutItem

Must be implemented in subclasses to return the layout item at index . If there is no such item, the function must return 0. 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.

def paintLayout(self, painter, item):
    layout = item.layout()
    if layout:
        for layout_item in layout:
            self.paintLayout(painter, layout_item)
    painter.drawRect(item.geometry())
def paintEvent(self, event):
    painter = QPainter(self)
    if self.layout():
        self.paintLayout(painter, self.layout())
											
PySide2.QtWidgets.QLayout. margin ( )
返回类型

int

另请参阅

setMargin()

PySide2.QtWidgets.QLayout. menuBar ( )
返回类型

QWidget

返回为此布局设置的菜单栏,或 None 若未设置菜单栏。

另请参阅

setMenuBar()

PySide2.QtWidgets.QLayout. parentWidget ( )
返回类型

QWidget

返回此布局的父级 Widget,或 None 若此布局未安装在任何 Widget 中。

若布局是子布局,此函数返回父级布局的父级 Widget。

另请参阅

parent()

PySide2.QtWidgets.QLayout. removeItem ( arg__1 )
参数

arg__1 QLayoutItem

移除布局项 item from the layout. It is the caller’s responsibility to delete the item.

预告: item 可以是布局 (由于 QLayout 继承 QLayoutItem ).

PySide2.QtWidgets.QLayout. removeWidget ( w )
参数

w QWidget

移除 Widget widget from 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.

注意

The ownership of widget remains the same as when it was added.

PySide2.QtWidgets.QLayout. replaceWidget ( from , to [ , options=Qt.FindChildrenRecursively ] )
参数
返回类型

QLayoutItem

搜索小部件 from and replaces it with widget to if found. Returns the layout item that contains the widget from on success. Otherwise None 被返回。若 options contains Qt::FindChildrenRecursively (the default), sub-layouts are searched for doing the replacement. Any other flag in options 被忽略。

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 from is no longer managed by the layout and may need to be deleted or hidden. The parent of widget from is left unchanged.

This function works for the built-in Qt layouts, but might not work for custom layouts.

另请参阅

indexOf()

PySide2.QtWidgets.QLayout. setAlignment ( l , alignment )
参数
返回类型

bool

这是重载函数。

Sets the alignment for the layout l to alignment 并返回 true if l 在此布局中被找到 (不包括子级布局);否则返回 false .

PySide2.QtWidgets.QLayout. setAlignment ( w , alignment )
参数
返回类型

bool

Sets the alignment for widget w to alignment and returns true if w 在此布局中被找到 (不包括子级布局);否则返回 false .

PySide2.QtWidgets.QLayout. setContentsMargins ( margins )
参数

margins QMargins

设置 margins 以围绕布局使用。

默认情况下, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

另请参阅

contentsMargins()

PySide2.QtWidgets.QLayout. setContentsMargins ( left , top , right , bottom )
参数
  • left int

  • top int

  • right int

  • bottom int

设置 left , top , right ,和 bottom margins to use around the layout.

默认情况下, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

另请参阅

contentsMargins() getContentsMargins() pixelMetric() PM_LayoutLeftMargin PM_LayoutTopMargin PM_LayoutRightMargin PM_LayoutBottomMargin

PySide2.QtWidgets.QLayout. setEnabled ( arg__1 )
参数

arg__1 bool

启用此布局,若 enable 为 true,否则禁用它。

启用的布局会根据变化动态调整;禁用的布局就好像它不存在一样。

默认情况下,所有布局是启用的。

另请参阅

isEnabled()

PySide2.QtWidgets.QLayout. setMargin ( arg__1 )
参数

arg__1 int

另请参阅

margin()

PySide2.QtWidgets.QLayout. setMenuBar ( w )
参数

w QWidget

Tells the geometry manager to place the menu bar widget at the top of parentWidget() , outside contentsMargins() . All child widgets are placed below the bottom edge of the menu bar.

另请参阅

menuBar()

PySide2.QtWidgets.QLayout. setSizeConstraint ( arg__1 )
参数

arg__1 SizeConstraint

另请参阅

sizeConstraint()

PySide2.QtWidgets.QLayout. setSpacing ( arg__1 )
参数

arg__1 int

另请参阅

spacing()

PySide2.QtWidgets.QLayout. sizeConstraint ( )
返回类型

SizeConstraint

PySide2.QtWidgets.QLayout. spacing ( )
返回类型

int

另请参阅

setSpacing()

PySide2.QtWidgets.QLayout. takeAt ( index )
参数

index int

返回类型

QLayoutItem

必须在子类中实现以移除布局项,在 index 从布局,并返回项。若没有这样的项,函数必须什么都不做,并返回 0。项从 0 起被连续编号。若项被移除,其它项将被重新编号。

下列代码片段展示从布局,安全移除所有项的办法:

child = layout.takeAt(0)
while child:
    ...
    del child
											
PySide2.QtWidgets.QLayout. totalHeightForWidth ( w )
参数

w int

返回类型

int

Also takes contentsMargins and menu bar into account.

PySide2.QtWidgets.QLayout. totalMaximumSize ( )
返回类型

QSize

Also takes contentsMargins and menu bar into account.

PySide2.QtWidgets.QLayout. totalMinimumSize ( )
返回类型

QSize

Also takes contentsMargins and menu bar into account.

PySide2.QtWidgets.QLayout. totalSizeHint ( )
返回类型

QSize

Also takes contentsMargins and menu bar into account.

PySide2.QtWidgets.QLayout. update ( )

更新布局为 parentWidget() .

通常,不需要调用此,因为它会被自动调用 (在最合适时间)。

另请参阅

activate() invalidate()

PySide2.QtWidgets.QLayout. widgetEvent ( arg__1 )
参数

arg__1 QEvent

Performs child widget layout when the parent widget is resized. Also handles removal of widgets. e is the event