Skip to content

FormLayout

Qt Base Class: QFormLayout

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

Base classes

Name Children Inherits
LayoutMixin
prettyqt.widgets.layout
QFormLayout
PySide6.QtWidgets
QFormLayout(self, parent: Optional[PySide6.QtWidgets.QWidget] \= None) -> None

⋔ Inheritance diagram

graph TD
  1473296337872["widgets.FormLayout"]
  1473296333968["widgets.LayoutMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473296346656["widgets.LayoutItemMixin"]
  1473365769904["QtWidgets.QFormLayout"]
  1473365768928["QtWidgets.QLayout"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473290791120["QtWidgets.QLayoutItem"]
  1473296333968 --> 1473296337872
  1473299815024 --> 1473296333968
  140713234304496 --> 1473299815024
  1473296346656 --> 1473296333968
  140713234304496 --> 1473296346656
  1473365769904 --> 1473296337872
  1473365768928 --> 1473365769904
  1473288842240 --> 1473365768928
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473290791120 --> 1473365768928
  1473291690208 --> 1473290791120

🛈 DocStrings

Bases: LayoutMixin, QFormLayout

Manages forms of input widgets and their associated labels.

Source code in prettyqt\widgets\formlayout.py
class FormLayout(widgets.LayoutMixin, widgets.QFormLayout):
    """Manages forms of input widgets and their associated labels."""

    ID = "form"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_size_constraint("maximum")
        self.setVerticalSpacing(8)

    def __setitem__(
        self, index: int | tuple[int, ItemRoleStr], value: str | widgets.QWidget
    ):
        match index:
            case (int() as row, str() as role):
                self.set_widget(value, row, role)
            case int() as row:
                self.set_widget(value, row, "both")
            case _:
                raise TypeError(index)

    def __delitem__(self, index: int):
        self.removeRow(index)

    def __iter__(self) -> Iterator[widgets.QWidget | widgets.QLayout]:
        return iter(self[i] for i in range(self.count()) if self[i] is not None)

    def __len__(self) -> int:
        """Needed for PySide2."""
        return self.rowCount()

    def __add__(self, other: widgets.QWidget | widgets.QLayout | tuple):
        self.add(other)
        return self

    def _get_map(self):
        maps = super()._get_map()
        maps |= {
            "FieldGrowthPolicy": FIELD_GROWTH_POLICY,
            "formAlignment": constants.ALIGNMENTS,
            "labelAlignment": constants.ALIGNMENTS,
            "rowWrapPolicy": ROW_WRAP_POLICY,
        }
        return maps

    def set_form_alignment(
        self, alignment: constants.AlignmentStr | constants.AlignmentFlag
    ):
        """Set the alignment of the form.

        Args:
            alignment: alignment for the form
        """
        self.setFormAlignment(constants.ALIGNMENTS.get_enum_value(alignment))

    def get_form_alignment(self) -> constants.AlignmentStr:
        """Return current form alignment.

        Returns:
            form alignment
        """
        return constants.ALIGNMENTS.inverse[self.formAlignment()]

    def set_label_alignment(
        self, alignment: constants.AlignmentStr | constants.AlignmentFlag
    ):
        """Set the alignment of the label.

        Args:
            alignment: alignment for the label
        """
        self.setFormAlignment(constants.ALIGNMENTS.get_enum_value(alignment))

    def get_label_alignment(self) -> constants.AlignmentStr:
        """Return current label alignment.

        Returns:
            label alignment
        """
        return constants.ALIGNMENTS.inverse[self.labelAlignment()]

    def set_widget(
        self,
        widget: str | widgets.QWidget,
        row: int,
        role: ItemRoleStr | mod.ItemRole = "both",
    ):
        widget = widgets.Label(widget) if isinstance(widget, str) else widget
        self.setWidget(row, ITEM_ROLE.get_enum_value(role), widget)

    def get_widget(
        self, row: int, role: ItemRoleStr | mod.ItemRole = "both"
    ) -> widgets.QLayout | widgets.QWidget:
        item = self.itemAt(row, ITEM_ROLE.get_enum_value(role))
        return i if (i := item.widget()) is not None else item.layout()

    def get_item_position(self, index: int) -> tuple[int, ItemRoleStr] | None:
        row, role = self.getItemPosition(index)  # type: ignore
        return None if row == -1 else (row, ITEM_ROLE.inverse[role])

    def add(self, *items):
        for i in items:
            match i:
                case widgets.QWidget() | widgets.QLayout():
                    self.addRow(i)
                case tuple():
                    self.addRow(*i)
                case _:
                    raise TypeError(i)

    def set_row_wrap_policy(self, policy: RowWrapPolicyStr | mod.RowWrapPolicy):
        """Set row wrap policy to use.

        Args:
            policy: row wrap policy to use
        """
        self.setRowWrapPolicy(ROW_WRAP_POLICY.get_enum_value(policy))

    def get_row_wrap_policy(self) -> RowWrapPolicyStr:
        """Return current row wrap policy.

        Returns:
            row wrap policy
        """
        return ROW_WRAP_POLICY.inverse[self.rowWrapPolicy()]

    def set_field_growth_policy(
        self, policy: FieldGrowthPolicyStr | mod.FieldGrowthPolicy
    ):
        """Set field growth policy to use.

        Args:
            policy: field growth policy to use
        """
        self.setFieldGrowthPolicy(FIELD_GROWTH_POLICY.get_enum_value(policy))

    def get_field_growth_policy(self) -> FieldGrowthPolicyStr:
        """Return current field growth policy.

        Returns:
            field growth policy
        """
        return FIELD_GROWTH_POLICY.inverse[self.fieldGrowthPolicy()]

__len__() -> int

Needed for PySide2.

Source code in prettyqt\widgets\formlayout.py
def __len__(self) -> int:
    """Needed for PySide2."""
    return self.rowCount()

get_field_growth_policy() -> FieldGrowthPolicyStr

Return current field growth policy.

Source code in prettyqt\widgets\formlayout.py
def get_field_growth_policy(self) -> FieldGrowthPolicyStr:
    """Return current field growth policy.

    Returns:
        field growth policy
    """
    return FIELD_GROWTH_POLICY.inverse[self.fieldGrowthPolicy()]

get_form_alignment() -> constants.AlignmentStr

Return current form alignment.

Source code in prettyqt\widgets\formlayout.py
def get_form_alignment(self) -> constants.AlignmentStr:
    """Return current form alignment.

    Returns:
        form alignment
    """
    return constants.ALIGNMENTS.inverse[self.formAlignment()]

get_label_alignment() -> constants.AlignmentStr

Return current label alignment.

Source code in prettyqt\widgets\formlayout.py
def get_label_alignment(self) -> constants.AlignmentStr:
    """Return current label alignment.

    Returns:
        label alignment
    """
    return constants.ALIGNMENTS.inverse[self.labelAlignment()]

get_row_wrap_policy() -> RowWrapPolicyStr

Return current row wrap policy.

Source code in prettyqt\widgets\formlayout.py
def get_row_wrap_policy(self) -> RowWrapPolicyStr:
    """Return current row wrap policy.

    Returns:
        row wrap policy
    """
    return ROW_WRAP_POLICY.inverse[self.rowWrapPolicy()]

set_field_growth_policy(policy: FieldGrowthPolicyStr | mod.FieldGrowthPolicy)

Set field growth policy to use.

Parameters:

Name Type Description Default
policy FieldGrowthPolicyStr | FieldGrowthPolicy

field growth policy to use

required
Source code in prettyqt\widgets\formlayout.py
def set_field_growth_policy(
    self, policy: FieldGrowthPolicyStr | mod.FieldGrowthPolicy
):
    """Set field growth policy to use.

    Args:
        policy: field growth policy to use
    """
    self.setFieldGrowthPolicy(FIELD_GROWTH_POLICY.get_enum_value(policy))

set_form_alignment(alignment: constants.AlignmentStr | constants.AlignmentFlag)

Set the alignment of the form.

Parameters:

Name Type Description Default
alignment AlignmentStr | AlignmentFlag

alignment for the form

required
Source code in prettyqt\widgets\formlayout.py
def set_form_alignment(
    self, alignment: constants.AlignmentStr | constants.AlignmentFlag
):
    """Set the alignment of the form.

    Args:
        alignment: alignment for the form
    """
    self.setFormAlignment(constants.ALIGNMENTS.get_enum_value(alignment))

set_label_alignment(alignment: constants.AlignmentStr | constants.AlignmentFlag)

Set the alignment of the label.

Parameters:

Name Type Description Default
alignment AlignmentStr | AlignmentFlag

alignment for the label

required
Source code in prettyqt\widgets\formlayout.py
def set_label_alignment(
    self, alignment: constants.AlignmentStr | constants.AlignmentFlag
):
    """Set the alignment of the label.

    Args:
        alignment: alignment for the label
    """
    self.setFormAlignment(constants.ALIGNMENTS.get_enum_value(alignment))

set_row_wrap_policy(policy: RowWrapPolicyStr | mod.RowWrapPolicy)

Set row wrap policy to use.

Parameters:

Name Type Description Default
policy RowWrapPolicyStr | RowWrapPolicy

row wrap policy to use

required
Source code in prettyqt\widgets\formlayout.py
def set_row_wrap_policy(self, policy: RowWrapPolicyStr | mod.RowWrapPolicy):
    """Set row wrap policy to use.

    Args:
        policy: row wrap policy to use
    """
    self.setRowWrapPolicy(ROW_WRAP_POLICY.get_enum_value(policy))

⌗ Property table

Qt Property Type Doc
objectName QString
spacing int
contentsMargins QMargins
sizeConstraint QLayout::SizeConstraint
fieldGrowthPolicy QFormLayout::FieldGrowthPolicy
rowWrapPolicy QFormLayout::RowWrapPolicy
labelAlignment QFlags
formAlignment QFlags
horizontalSpacing int
verticalSpacing int