Skip to content

MultiLineLayout

Qt Base Class: QBoxLayout

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

Base classes

Name Children Inherits
BoxLayout
prettyqt.widgets.boxlayout

⋔ Inheritance diagram

graph TD
  1473367029648["custom_widgets.MultiLineLayout"]
  1473296355440["widgets.BoxLayout"]
  1473296351536["widgets.BoxLayoutMixin"]
  1473296333968["widgets.LayoutMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473296346656["widgets.LayoutItemMixin"]
  1473365797232["QtWidgets.QBoxLayout"]
  1473365768928["QtWidgets.QLayout"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473290791120["QtWidgets.QLayoutItem"]
  1473296355440 --> 1473367029648
  1473296351536 --> 1473296355440
  1473296333968 --> 1473296351536
  1473299815024 --> 1473296333968
  140713234304496 --> 1473299815024
  1473296346656 --> 1473296333968
  140713234304496 --> 1473296346656
  1473365797232 --> 1473296355440
  1473365768928 --> 1473365797232
  1473288842240 --> 1473365768928
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473290791120 --> 1473365768928
  1473291690208 --> 1473290791120

🛈 DocStrings

Bases: BoxLayout

Nested Boxlayout.

Source code in prettyqt\custom_widgets\layouts\multilinelayout.py
class MultiLineLayout(widgets.BoxLayout):
    """Nested Boxlayout."""

    def __init__(self, vertical: bool = True, row_number: int = 3, **kwargs):
        self.row_nb = row_number
        self.layouts = []
        self._sub_layout_type = "box"
        direction = self.Direction.TopToBottom if vertical else self.Direction.LeftToRight
        super().__init__(direction, **kwargs)

    def set_direction(
        self, direction: widgets.BoxLayout.Direction | widgets.boxlayout.DirectionStr
    ):
        super().set_direction(direction)
        direction = self.get_sub_direction()
        for layout in self.layouts:
            if isinstance(layout, widgets.QBoxLayout | MultiLineLayout):
                layout.set_direction(direction)

    def get_sub_direction(self) -> widgets.BoxLayout.Direction:
        return (
            self.Direction.LeftToRight
            if self.get_direction() == "top_to_bottom"
            else self.Direction.TopToBottom
        )

    def _add_sub_layout(self):
        match self._sub_layout_type:
            case "box":
                direction = self.get_sub_direction()
                layout = widgets.BoxLayout(direction)
            case "grid":
                layout = widget.GridLayout()
            case "flow":
                layout = custom_widgets.FlowLayout()
            case "stacked":
                layout = widgets.StackedLayout()
            case "nested":
                direction = self.get_sub_direction()
                layout = MultiLineLayout(direction)
        super().addLayout(layout)
        self.layouts.append(layout)

    def addWidget(self, widget: widgets.QWidget, **kwargs):
        if not self.layouts or self.layouts[-1].count() == self.row_nb:
            self._add_sub_layout()
        self.layouts[-1].add(widget, **kwargs)

    def addLayout(self, layout: widgets.QLayout):
        if not self.layouts or self.layouts[-1].count() == self.row_nb:
            self._add_sub_layout()
        self.layouts[-1].add(layout)

    def addItem(self, item):
        if not self.layouts or self.layouts[-1].count() == self.row_nb:
            self._add_sub_layout()
        self.layouts[-1].add(item)

    def get_items(self) -> listdelegators.ListDelegator[widgets.QLayoutItem]:
        items = [i.get_items() for i in self.layouts]
        return listdelegators.ListDelegator(itertools.chain(*items))

    def itemAt(self, idx: int) -> widgets.QLayoutItem | None:
        if len(self.layouts) == 0 or len(self.get_items()) == 0:
            raise IndexError(idx)
        # doesnt seem right?
        return None if idx == len(self.get_items()) else self.get_items()[idx]

    def takeAt(self, idx: int) -> widgets.QLayoutItem | None:  # or 0 according to docs?
        layout_idx, item_idx = divmod(idx, len(self.layouts))
        layout = self.layouts[layout_idx]
        item = layout.takeAt(item_idx)
        for i, layout in enumerate(self.layouts[layout_idx:-1], start=layout_idx):
            item = self.layouts[i + 1].takeAt(0)
            layout.addItem(item)
        if len(self.layouts[-1]) == 0:
            super().takeAt(super().count() - 1)
        return item

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

    def indexOf(self, item: widgets.QLayoutItem) -> int:
        return self.get_items().index(item)

    def set_sub_layout_type(self, layout_type: str):
        self._sub_layout_type = layout_type

    def get_sub_layout_type(self) -> str:
        return self._sub_layout_type

    sub_layout_type = core.Property(
        str,
        get_sub_layout_type,
        set_sub_layout_type,
        doc="Layout for the sub layouts",
    )

⌗ Property table

Qt Property Type Doc
objectName QString
spacing int
contentsMargins QMargins
sizeConstraint QLayout::SizeConstraint
sub_layout_type QString Layout for the sub layouts