Skip to content

SizePolicy

Qt Base Class: QSizePolicy

Signature: QSizePolicy(self) -> None QSizePolicy(self, horizontal: PySide6.QtWidgets.QSizePolicy.Policy, vertical: PySide6.QtWidgets.QSizePolicy.Policy, type: PySide6.QtWidgets.QSizePolicy.ControlType = Instance(PySide6.QtWidgets.QSizePolicy.ControlType.DefaultType)) -> None

Base classes

Name Children Inherits
QSizePolicy
PySide6.QtWidgets
QSizePolicy(self) -> None

⋔ Inheritance diagram

graph TD
  1473293671648["widgets.SizePolicy"]
  1473290782336["QtWidgets.QSizePolicy"]
  1473291690208["Shiboken.Object"]
  140713234304496["builtins.object"]
  1473290782336 --> 1473293671648
  1473291690208 --> 1473290782336
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: QSizePolicy

Layout attribute describing horizontal and vertical resizing policy.

Source code in prettyqt\widgets\sizepolicy.py
class SizePolicy(QtWidgets.QSizePolicy):
    """Layout attribute describing horizontal and vertical resizing policy."""

    def __init__(self, *args, **kwargs):
        match args:
            case (str(), str()):
                super().__init__(SIZE_POLICY[args[0]], SIZE_POLICY[args[1]])
            case (str(), str(), str()):
                super().__init__(
                    SIZE_POLICY[args[0]], SIZE_POLICY[args[1]], CONTROL_TYPE[args[2]]
                )
            case _:
                super().__init__(*args, **kwargs)

    def __repr__(self) -> str:
        return get_repr(
            self,
            self.get_horizontal_policy(),
            self.get_vertical_policy(),
            self.get_control_type(),
        )

    def __getstate__(self):
        return dict(
            has_height_for_width=self.hasHeightForWidth(),
            has_width_for_height=self.hasWidthForHeight(),
            horizontal_stretch=self.horizontalStretch(),
            vertical_stretch=self.verticalStretch(),
            horizontal_policy=self.get_horizontal_policy(),
            vertical_policy=self.get_vertical_policy(),
            retain_size_when_hidden=self.retainSizeWhenHidden(),
            control_type=self.get_control_type(),
        )

    def __setstate__(self, state: dict[str, Any]):
        self.setHeightForWidth(state["has_height_for_width"])
        self.setWidthForHeight(state["has_width_for_height"])
        self.setHorizontalStretch(state["horizontal_stretch"])
        self.setVerticalStretch(state["vertical_stretch"])
        self.set_horizontal_policy(state["horizontal_policy"])
        self.set_vertical_policy(state["vertical_policy"])
        self.setRetainSizeWhenHidden(state["retain_size_when_hidden"])
        self.set_control_type(state["control_type"])

    def __reduce__(self):
        return type(self), (), self.__getstate__()

    def serialize(self) -> dict[str, Any]:
        return self.__getstate__()

    @classmethod
    def clone(cls, qpol: QtWidgets.QSizePolicy) -> Self:
        pol = cls(qpol.horizontalPolicy(), qpol.verticalPolicy(), qpol.controlType())
        pol.setHeightForWidth(qpol.hasHeightForWidth())
        pol.setWidthForHeight(qpol.hasWidthForHeight())
        pol.setHorizontalStretch(qpol.horizontalStretch())
        pol.setVerticalStretch(qpol.verticalStretch())
        pol.setRetainSizeWhenHidden(qpol.retainSizeWhenHidden())
        return pol

    def get_horizontal_policy(self) -> SizePolicyStr:
        """Return size policy.

        Returns:
            horizontal size policy
        """
        return SIZE_POLICY.inverse[self.horizontalPolicy()]

    def set_horizontal_policy(self, policy: SizePolicyStr | QtWidgets.QSizePolicy.Policy):
        """Set the horizontal policy.

        Args:
            policy: policy to set
        """
        self.setHorizontalPolicy(SIZE_POLICY.get_enum_value(policy))

    def get_vertical_policy(self) -> SizePolicyStr:
        """Return size policy.

        Returns:
            vertical size policy

        """
        return SIZE_POLICY.inverse[self.verticalPolicy()]

    def set_vertical_policy(self, policy: SizePolicyStr | QtWidgets.QSizePolicy.Policy):
        """Set the horizontal policy.

        Args:
            policy: policy to set
        """
        self.setVerticalPolicy(SIZE_POLICY.get_enum_value(policy))

    def get_control_type(self) -> ControlTypeStr:
        """Return control type.

        Returns:
            control type
        """
        return CONTROL_TYPE.inverse[self.controlType()]

    def set_control_type(self, typ: ControlTypeStr | QtWidgets.QSizePolicy.ControlType):
        """Set the control type.

        Args:
            typ: control type to set
        """
        self.setControlType(CONTROL_TYPE.get_enum_value(typ))

    def get_transposed(self) -> Self:
        transposed = self.transposed()
        return type(self).clone(transposed)

get_control_type() -> ControlTypeStr

Return control type.

Source code in prettyqt\widgets\sizepolicy.py
def get_control_type(self) -> ControlTypeStr:
    """Return control type.

    Returns:
        control type
    """
    return CONTROL_TYPE.inverse[self.controlType()]

get_horizontal_policy() -> SizePolicyStr

Return size policy.

Source code in prettyqt\widgets\sizepolicy.py
def get_horizontal_policy(self) -> SizePolicyStr:
    """Return size policy.

    Returns:
        horizontal size policy
    """
    return SIZE_POLICY.inverse[self.horizontalPolicy()]

get_vertical_policy() -> SizePolicyStr

Return size policy.

Source code in prettyqt\widgets\sizepolicy.py
def get_vertical_policy(self) -> SizePolicyStr:
    """Return size policy.

    Returns:
        vertical size policy

    """
    return SIZE_POLICY.inverse[self.verticalPolicy()]

set_control_type(typ: ControlTypeStr | QtWidgets.QSizePolicy.ControlType)

Set the control type.

Parameters:

Name Type Description Default
typ ControlTypeStr | ControlType

control type to set

required
Source code in prettyqt\widgets\sizepolicy.py
def set_control_type(self, typ: ControlTypeStr | QtWidgets.QSizePolicy.ControlType):
    """Set the control type.

    Args:
        typ: control type to set
    """
    self.setControlType(CONTROL_TYPE.get_enum_value(typ))

set_horizontal_policy(policy: SizePolicyStr | QtWidgets.QSizePolicy.Policy)

Set the horizontal policy.

Parameters:

Name Type Description Default
policy SizePolicyStr | Policy

policy to set

required
Source code in prettyqt\widgets\sizepolicy.py
def set_horizontal_policy(self, policy: SizePolicyStr | QtWidgets.QSizePolicy.Policy):
    """Set the horizontal policy.

    Args:
        policy: policy to set
    """
    self.setHorizontalPolicy(SIZE_POLICY.get_enum_value(policy))

set_vertical_policy(policy: SizePolicyStr | QtWidgets.QSizePolicy.Policy)

Set the horizontal policy.

Parameters:

Name Type Description Default
policy SizePolicyStr | Policy

policy to set

required
Source code in prettyqt\widgets\sizepolicy.py
def set_vertical_policy(self, policy: SizePolicyStr | QtWidgets.QSizePolicy.Policy):
    """Set the horizontal policy.

    Args:
        policy: policy to set
    """
    self.setVerticalPolicy(SIZE_POLICY.get_enum_value(policy))