Skip to content

BorderLayout

Qt Base Class: QLayout

Signature: QLayout(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None

Base classes

Name Children Inherits
Layout
prettyqt.widgets.layout

⋔ Inheritance diagram

graph TD
  1473367028672["custom_widgets.BorderLayout"]
  1473296341776["widgets.Layout"]
  1473296333968["widgets.LayoutMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473296346656["widgets.LayoutItemMixin"]
  1473365768928["QtWidgets.QLayout"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473290791120["QtWidgets.QLayoutItem"]
  1473296341776 --> 1473367028672
  1473296333968 --> 1473296341776
  1473299815024 --> 1473296333968
  140713234304496 --> 1473299815024
  1473296346656 --> 1473296333968
  140713234304496 --> 1473296346656
  1473365768928 --> 1473296341776
  1473288842240 --> 1473365768928
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473290791120 --> 1473365768928
  1473291690208 --> 1473290791120

🛈 DocStrings

Bases: Layout

Layout based on 5 Regions (north east, south, west and center).

Source code in prettyqt\custom_widgets\layouts\borderlayout.py
class BorderLayout(widgets.Layout):
    """Layout based on 5 Regions (north east, south, west and center)."""

    ID = "border"

    class Position(enum.IntEnum):
        """Item position."""

        West = 0
        North = 1
        South = 2
        East = 3
        Center = 4

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.items: list[ItemWrapper] = []

    def addItem(self, item: widgets.QWidgetItem):
        self.add_widgetitem(item, BorderLayout.Position.West)

    def addWidget(
        self,
        widget: widgets.QWidget,
        position: Position | None = None,
    ):
        position = BorderLayout.Position.West if position is None else position
        self.add_widgetitem(widgets.WidgetItem(widget), position)

    def expandingDirections(self) -> constants.Orientation:
        return constants.HORIZONTAL | constants.VERTICAL

    def hasHeightForWidth(self) -> bool:
        return False

    def count(self) -> int:
        return len(self.items)

    def itemAt(self, index: int) -> widgets.QWidgetItem | None:
        return self.items[index].item if index < len(self.items) else None

    def minimumSize(self):
        return self.calculate_size("minimum")

    def setGeometry(self, rect: core.QRect):
        center = None
        east_width = 0
        west_width = 0
        north_height = 0
        south_height = 0
        super().setGeometry(rect)
        for wrapper in self.items:
            item = wrapper.item
            match wrapper.position:
                case BorderLayout.Position.North:
                    h = item.sizeHint().height()
                    geom = core.Rect(rect.x(), north_height, rect.width(), h)
                    item.setGeometry(geom)
                    north_height += item.geometry().height() + self.spacing()
                case BorderLayout.Position.South:
                    geo = item.geometry()
                    h = item.sizeHint().height()
                    geom = core.Rect(geo.x(), geo.y(), rect.width(), h)
                    item.setGeometry(geom)
                    south_height += item.geometry().height() + self.spacing()
                    y = rect.y() + rect.height() - south_height + self.spacing()
                    geo = item.geometry()
                    geom = core.Rect(rect.x(), y, geo.width(), geo.height())
                    item.setGeometry(geom)
                case BorderLayout.Position.Center:
                    center = wrapper

        center_height = rect.height() - north_height - south_height

        for wrapper in self.items:
            item = wrapper.item
            match wrapper.position:
                case BorderLayout.Position.West:
                    x = rect.x() + west_width
                    w = item.sizeHint().width()
                    geom = core.Rect(x, north_height, w, center_height)
                    item.setGeometry(geom)
                    west_width += item.geometry().width() + self.spacing()
                case BorderLayout.Position.East:
                    geo = item.geometry()
                    w = item.sizeHint().width()
                    geom = core.Rect(geo.x(), geo.y(), w, center_height)
                    item.setGeometry(geom)
                    east_width += item.geometry().width() + self.spacing()
                    x = rect.x() + rect.width() - east_width + self.spacing()
                    geom = core.Rect(x, north_height, geo.width(), geo.height())
                    item.setGeometry(geom)

        if center:
            w = rect.width() - east_width - west_width
            rect = core.Rect(west_width, north_height, w, center_height)
            center.item.setGeometry(rect)

    def sizeHint(self) -> core.Size:
        return self.calculate_size("size_hint")

    def takeAt(self, index: int) -> widgets.QWidgetItem | None:
        if 0 <= index < len(self.items):
            layout_struct = self.items.pop(index)
            return layout_struct.item

        return None

    def add_widgetitem(self, item: widgets.QWidgetItem, position: Position):
        self.items.append(ItemWrapper(item, position))

    def calculate_size(self, size_type: Literal["minimum", "size_hint"]) -> core.Size:
        total_size = core.Size()
        Pos = BorderLayout.Position
        for wrapper in self.items:
            item_size = (
                wrapper.item.minimumSize()
                if size_type == "minimum"
                else wrapper.item.sizeHint()
            )
            if wrapper.position in (Pos.North, Pos.South, Pos.Center):
                total_size.setHeight(total_size.height() + item_size.height())
            if wrapper.position in (Pos.West, Pos.East, Pos.Center):
                total_size.setWidth(total_size.width() + item_size.width())
        return total_size

Position

Bases: IntEnum

Item position.

Source code in prettyqt\custom_widgets\layouts\borderlayout.py
class Position(enum.IntEnum):
    """Item position."""

    West = 0
    North = 1
    South = 2
    East = 3
    Center = 4

⌗ Property table

Qt Property Type Doc
objectName QString
spacing int
contentsMargins QMargins
sizeConstraint QLayout::SizeConstraint