PyQt5布局管理

1.如何进行一个布局管理

# 1.创建一个布局对象
layout = QLayout() # 不需要设置父对象

# 2.设置布局对象参数
# margin
setContentsMargins(self, int, int, int, int) # 左上右下
# spacing
setSpacing(self, int)
# alignment
setAlignment(self, w: Optional[QWidget], alignment: Union[Qt.Alignment, Qt.AlignmentFlag]) -> bool
setAlignment(self, l: Optional[QLayout], alignment: Union[Qt.Alignment, Qt.AlignmentFlag]) -> bool
setAlignment(self, alignment: Union[Qt.Alignment, Qt.AlignmentFlag])

# 3.设置给需要布局子控件的父控件调整方向
QWidget.setLayout(QLayout)
QWidget.setLayoutDirection(Qt.LayoutDirection)
"""
Qt.LayoutDirection
	LeftToRight 从左到右
	RightToLeft 从右到左
	LayoutDirectionAuto # 自动布局
"""
QWidget.unsetLayoutDirection()

# 4.将布局控件内部的子空间添加到布局管理器中自动进行布局

案例

from PyQt5.Qt import *
import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("案例")
        self.resize(500, 500)
        self.rb = QRubberBand(QRubberBand.Rectangle, self)
        self.setup_ui()

    def setup_ui(self):
        label1 = QLabel(self)
        label1.setText("label1")
        label1.setStyleSheet("background-color: red")

        label2 = QLabel(self)
        label2.setText("label2")
        label2.setStyleSheet("background-color: green")

        label3 = QLabel(self)
        label3.setText("label3")
        label3.setStyleSheet("background-color: blue")

        layout = QHBoxLayout()
        layout.addWidget(label1)
        layout.addWidget(label2)
        layout.addWidget(label3)

        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()

    window.show()

    sys.exit(app.exec_())

2.QLayout

# 布局管理器的抽象类
# 控制控件间距
setSpacing(int)
spacing() -> int

# 外边距
setContentsMargins(self, left: int, top: int, right: int, bottom: int)

# 添加子控件
addWidget(QWidget)

# 替换子控件
replaceWidget(self, from_: Optional[QWidget], to: Optional[QWidget], options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) -> Optional[QLayoutItem]

# 添加子布局
addLayout(QLayout layout)

# 能用性
isEnabled() -> bool
setEnabled(bool)

3.QBoxLayout

# 提供水平或垂直方向的布局管理器
# 构造函数
QBoxLayout(direction: QBoxLayout.Direction, parent: Optional[QWidget] = None) # parent一般不写
"""
QBoxLayout.Direction
	QBoxLayout.LeftToRight 从左到右
	QBoxLayout.BottomToTop 从下到上
    QBoxLayout.RightToLeft 从右到左
    QBoxLayout.TopToBottom 从上到下
"""

# 修改方向
setDirection(QBoxLayout.Direction)
direction -> QBoxLayout.Direction

# 添加元素
# 添加控件 stretch可以表示占整个父组件的比例
addWidget(self, a0: Optional[QWidget], stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment())
insertWidget(self, index: int, widget: Optional[QWidget], stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment())

# 添加子布局
addLayout(self, layout: Optional[QLayout], stretch: int = 0)
insertLayout(self, index: int, layout: Optional[QLayout], stretch: int = 0)

# 替换控件
replaceWidget(self, from_: Optional[QWidget], to: Optional[QWidget], options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) -> Optional[QLayoutItem]

# 移除控件
removeWidget(self, w: Optional[QWidget])
# 可以考虑使用QWidget.setParent(None)
QWidget.hide()

# 添加空白
addSpacing(self, size: int)
insertSpacing(self, index: int, size: int)

# 添加空白伸缩(弹簧)
addStretch(self, stretch: int = 0)
insertStretch(self, index: int, stretch: int = 0)

# 设置伸缩(给某个控件)
setStretchFactor(self, w: Optional[QWidget], stretch: int) -> bool # 用于控件
setStretchFactor(self, l: Optional[QLayout], stretch: int) -> bool # 用于布局

# 边距
setContentsMargins(self, left: int, top: int, right: int, bottom: int)
setSpacing(int)

这个函数具有两个子类:QHBoxLayoutQVBoxLayout

这两个子类只在构造函数中确定了水平方向和垂直方向

4.QFormLayout

# 提供表单布局

# 构造函数
QFormLayout(parent: Optional[QWidget] = None)

# 行操作
# 添加行
addRow(self, label: Optional[QWidget], field: Optional[QWidget])
addRow(self, label: Optional[QWidget], field: Optional[QLayout])
addRow(self, labelText: Optional[str], field: Optional[QWidget])
addRow(self, labelText: Optional[str], field: Optional[QLayout])
addRow(self, widget: Optional[QWidget])
addRow(self, layout: Optional[QLayout])

# 插入行
insertRow(self, row: int, label: Optional[QWidget], field: Optional[QWidget])
insertRow(self, row: int, label: Optional[QWidget], field: Optional[QLayout])
insertRow(self, row: int, labelText: Optional[str], field: Optional[QWidget])
insertRow(self, row: int, labelText: Optional[str], field: Optional[QLayout])
insertRow(self, row: int, widget: Optional[QWidget])
insertRow(self, row: int, layout: Optional[QLayout])

# 获取行信息
getWidgetPosition(self, widget: Optional[QWidget]) -> (Optional[int], Optional[QFormLayout.ItemRole])
getLayoutPosition(self, layout: Optional[QLayout]) -> (Optional[int], Optional[QFormLayout.ItemRole])
rowCount() -> int #  总行数

# 修改行
setLayout(self, row: int, role: QFormLayout.ItemRole, layout: Optional[QLayout])
setWidget(self, row: int, role: QFormLayout.ItemRole, widget: Optional[QWidget])
"""
QFormLayout.ItemRole
    QFormLayout.LabelRole 标签
    QFormLayout.FieldRole 输入框
    QFormLayout.SpanningRole 跨越标签和输入框的控件
"""

# 移除行(删除子控件)
removeRow(self, int)
removeRow(self, QWidget)
removeRow(self, QLayout)

# 移除行(不删除子控件)
takeRow(self, int) -> QFormLayout.TakeRowResult
takeRow(self, QWidget) -> QFormLayout.TakeRowResult
takeRow(self, QLayout) -> QFormLayout.TakeRowResult

# 标签操作
labelForField(self, field: Optional[QWidget]) -> Optional[QWidget]
labelForField(self, field: Optional[QLayout]) -> Optional[QWidget]

# 行的包装策略
setRowWrapPolicy(self, policy: QFormLayout.RowWrapPolicy)
rowWrapPolicy(self) -> QFormLayout.RowWrapPolicy
"""
QFormLayout.RowWrapPolicy
    DontWrapRows 字段总放在标签旁
    WrapAllRows 标签宽度为最宽标签
    WrapLongRows 字段总是位于标签下方
"""

# 对齐方式
setFormAlignment(self, alignment: Union[Qt.Alignment, Qt.AlignmentFlag])
formAlignment(self) -> Qt.Alignment
setLabelAlignment(self, alignment: Union[Qt.Alignment, Qt.AlignmentFlag])
labelAlignment(self) -> Qt.Alignment

# 间距
setVerticalSpacing(self, spacing: int)
verticalSpacing(self) -> int
setHorizontalSpacing(self, spacing: int)
horizontalSpacing(self) -> int
spacing(self) -> int

# 字段的增长策略
setFieldGrowthPolicy(self, policy: QFormLayout.FieldGrowthPolicy)
fieldGrowthPolicy(self) -> QFormLayout.FieldGrowthPolicy
"""
QFormLayout.FieldsStayAtSizeHint 控件大小保持其原始建议大小,不会随着布局调整而变化。
QFormLayout.ExpandingFieldsGrow 控件会根据布局的可用空间自动扩展。
QFormLayout.AllNonFixedFieldsGrow 所有没有固定大小的控件会根据布局空间进行增长。
"""

5.QGridLayout

# 网格布局
# 构造函数
QGridLayout(QWidget)
QGridLayout()

# 元素操作
# 控件
addWidget(self, w: Optional[QWidget])
addWidget(self, a0: Optional[QWidget], row: int, column: int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment())
addWidget(self, a0: Optional[QWidget], row: int, column: int, rowSpan: int, columnSpan: int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment())
# 布局
addLayout(self, a0: Optional[QLayout], row: int, column: int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment())
addLayout(self, a0: Optional[QLayout], row: int, column: int, rowSpan: int, columnSpan: int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment())
# 获取
# 位置获取
getItemPosition(self, idx: int) -> (Optional[int], Optional[int], Optional[int], Optional[int])
# 条目获取
itemAtPosition(self, row: int, column: int) -> Optional[QLayoutItem]

# 列宽/行高和拉伸系数
setColumnMinimumWidth(self, column: int, minSize: int)
columnMinimumWidth(self, column: int)
setColumnStretch(self, column: int, stretch: int)
columnStretch(self, column: int)
setRowMinimumHeight(self, row: int, minSize: int)
rowMinimumHeight(self, row: int) -> int
setRowStretch(self, row: int, stretch: int)
rowStretch(self, row: int) -> int

# 间距
setVerticalSpacing(self, spacing: int)
verticalSpacing(self) -> int
setHorizontalSpacing(self, spacing: int)
horizontalSpacing(self) -> int
setSpacing(self, spacing: int)
spacing(self) -> int

# 原点位置
setOriginCorner(self, a0: Qt.Corner)
originCorner(self) -> Qt.Corner
"""
TopLeftCorner 左上
TopRightCorner 右上
BottomLeftCorner 左下
BottomRightCorner 右下
"""

# 信息获取
cellRect(self, row: int, column: int) -> QRect
columnCount(self) -> int
rowCount(self) -> int

6.QStackLayout

# 提供一个堆叠起来的布局,在同一时间只能显示一个控件
# 构造函数
QStackedLayout()
QStackedLayout(parent: Optional[QWidget])
QStackedLayout(parentLayout: Optional[QLayout])

# 添加子控件
addWidget(self, QWidget) -> int
insertWidget(self, int, QWidget) -> int

# 获取子控件
widget(int index) -> QWidget

# 切换
setCurrentIndex(self, index: int)
currentIndex(self) -> int
setCurrentWidget(self, w: Optional[QWidget])
currentWidget(self) -> Optional[QWidget]

# 展示模式
setStackingMode(self, stackingMode: QStackedLayout.StackingMode)
stackingMode(self) -> QStackedLayout.StackingMode
"""
QStackedLayout.StackingMode
    QStackedLayout.StackAll 显示所有控件,当前控件显示在最前面
    QStackedLayout.StackOne 所有部件都可见
"""
# 信号
currentChanged(int index)
widgetRemoved(int index)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值