Skip to content

StandardItem

Qt Base Class: QStandardItem

Signature: QStandardItem(self) -> None QStandardItem(self, icon: Union[PySide6.QtGui.QIcon, PySide6.QtGui.QPixmap], text: str) -> None QStandardItem(self, other: PySide6.QtGui.QStandardItem) -> None QStandardItem(self, rows: int, columns: int = 1) -> None QStandardItem(self, text: str) -> None

Base classes

Name Children Inherits
SerializeMixin
prettyqt.utils.serializemixin
QStandardItem
PySide6.QtGui
QStandardItem(self) -> None

â‹” Inheritance diagram

graph TD
  1473245634368["gui.StandardItem"]
  1473299806240["utils.SerializeMixin"]
  140713234304496["builtins.object"]
  1473300039424["QtGui.QStandardItem"]
  1473291690208["Shiboken.Object"]
  1473299806240 --> 1473245634368
  140713234304496 --> 1473299806240
  1473300039424 --> 1473245634368
  1473291690208 --> 1473300039424
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: SerializeMixin, QStandardItem

Source code in prettyqt\gui\standarditem.py
class StandardItem(serializemixin.SerializeMixin, gui.QStandardItem):
    def __repr__(self):
        return get_repr(self, self.get_icon(), self.text())

    def __getitem__(
        self, index: int | slice | tuple[int | slice, int | slice] | core.QModelIndex
    ) -> gui.QStandardItem | listdelegators.ListDelegator[gui.QStandardItem]:
        match index:
            case int():
                if index >= self.childCount():
                    raise IndexError(index)
                return self.child(index)
            case slice():
                return self.__getitem__(index, 0)
            case int() as row, int() as col:
                return self.child(row, col)
            case (row, col):
                rowcount = self.rowCount()
                colcount = self.columnCount()
                children = [
                    self.child(i, j)
                    for i, j in helpers.iter_positions(row, col, rowcount, colcount)
                ]
                return listdelegators.ListDelegator(children)
            case _:
                raise TypeError(index)

    def __delitem__(self, index: int | tuple[int, int]):
        item = self.takeRow(index) if isinstance(index, int) else self.takeChild(*index)
        if item is None:
            raise KeyError(index)
        return item

    def __iter__(self) -> Iterator[gui.QStandardItem]:
        return iter(self.get_children())

    def __add__(self, other: str | gui.QStandardItem) -> StandardItem:
        match other:
            case gui.QStandardItem() | str():
                self.add(other)
                return self
            case _:
                raise TypeError("wrong type for addition")

    def get_children(self) -> listdelegators.ListDelegator[gui.QStandardItem]:
        items = [self.child(index) for index in range(self.rowCount())]
        return listdelegators.ListDelegator(items)

    def add(self, *item: str | gui.QStandardItem):
        for i in item:
            new_item = type(self)(i) if isinstance(i, str) else i
            self.appendRow([new_item])

    def clone(self) -> Self:
        item = type(self)()
        core.DataStream.copy_data(self, item)
        return item

    def set_icon(self, icon: datatypes.IconType):
        """Set the icon for the action.

        Args:
            icon: icon to use
        """
        icon = iconprovider.get_icon(icon)
        self.setIcon(icon)

    def set_checkstate(
        self, state: constants.CheckStateStr | constants.CheckState | bool
    ):
        """Set checkstate of the checkbox.

        Args:
            state: checkstate to use
        """
        if isinstance(state, bool):
            state = (
                constants.CheckState.Checked if state else constants.CheckState.Unchecked
            )
        self.setCheckState(constants.CHECK_STATE.get_enum_value(state))

    def is_checked(self):
        return self.checkState() == constants.CheckState.Checked

    def toggle_checkstate(self):
        if self.checkState() == constants.CheckState.Checked:
            self.setCheckState(constants.CheckState.Unchecked)
        else:
            self.setCheckState(constants.CheckState.Checked)

    def get_checkstate(self) -> constants.CheckStateStr:
        """Return checkstate.

        Returns:
            checkstate
        """
        return constants.CHECK_STATE.inverse[self.checkState()]

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

        Args:
            alignment: alignment for the format
        """
        self.setTextAlignment(constants.ALIGNMENTS.get_enum_value(alignment))

    def get_text_alignment(self) -> constants.AlignmentStr:
        """Return current text alignment.

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

    def get_background(self) -> gui.Brush:
        return gui.Brush(self.background())

    def get_foreground(self) -> gui.Brush:
        return gui.Brush(self.foreground())

    def get_font(self) -> gui.Font:
        return gui.Font(self.font())

    def get_icon(self) -> gui.Icon | None:
        icon = self.icon()
        return None if icon.isNull() else gui.Icon(icon)

    def set_data(self, data: Any, role: constants.ItemDataRoleStr | int):
        item_role = constants.ITEM_DATA_ROLE[role] if isinstance(role, str) else role
        super().setData(data, item_role)

    def get_data(self, role: constants.ItemDataRoleStr | int):
        item_role = constants.ITEM_DATA_ROLE[role] if isinstance(role, str) else role
        return super().data(item_role)

    def set_tooltip(
        self,
        tooltip: str | datatypes.PathType,
        size: datatypes.SizeType | None = None,
        rich_text: bool = False,
    ):
        if isinstance(tooltip, os.PathLike):
            path = os.fspath(tooltip)
            if size is None:
                tooltip = f"<img src={path!r}>"
            else:
                s = datatypes.to_size(size)
                tooltip = f'<img src={path!r} width="{s.width()}" height="{s.height()}">'
        if rich_text:
            tooltip = f"<html>{html.escape(tooltip)}</html>"
        super().setToolTip(tooltip)

    def set_size_hint(self, hint: datatypes.SizeType):
        self.setSizeHint(datatypes.to_size(hint))

    def add_item(
        self,
        name: str = "",
        icon: datatypes.IconType = None,
        data: dict | None = None,
        foreground: gui.QBrush | None = None,
        background: gui.QBrush | None = None,
        font: gui.QFont | None = None,
        selectable: bool = True,
        enabled: bool = True,
        editable: bool = False,
        status_tip: str = "",
        tool_tip: str = "",
        whats_this: str | None = None,
        # text_alignment: Optional[str] = None,
        checkstate: constants.CheckStateStr | None = None,
        flags: constants.ItemFlag | None = None,
        size_hint: datatypes.SizeType | None = None,
        is_user_type: bool = False,
    ) -> Self:
        item = type(self)(name)
        if icon is not None:
            icon = iconprovider.get_icon(icon)
            item.setIcon(icon)
        if data is not None:
            for k, v in data.items():
                item.set_data(v, k)
        if foreground is not None:
            item.setForeground(foreground)
        if background is not None:
            item.setBackground(background)
        if font is not None:
            item.setFont(font)
        if flags is not None:
            item.setFlags(flags)
        if enabled:
            item.setEnabled(enabled)
        if editable:
            item.setEditable(editable)
        if selectable:
            item.setSelectable(selectable)
        if status_tip:
            item.setStatusTip(status_tip)
        if tool_tip:
            item.setToolTip(tool_tip)
        if whats_this:
            item.setWhatsThis(whats_this)
        if size_hint is not None:
            item.set_size_hint(size_hint)
        if checkstate is not None:
            item.set_checkstate(checkstate)
        self.appendRow([item])
        return item

get_checkstate() -> constants.CheckStateStr

Return checkstate.

Source code in prettyqt\gui\standarditem.py
def get_checkstate(self) -> constants.CheckStateStr:
    """Return checkstate.

    Returns:
        checkstate
    """
    return constants.CHECK_STATE.inverse[self.checkState()]

get_text_alignment() -> constants.AlignmentStr

Return current text alignment.

Source code in prettyqt\gui\standarditem.py
def get_text_alignment(self) -> constants.AlignmentStr:
    """Return current text alignment.

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

set_checkstate(state: constants.CheckStateStr | constants.CheckState | bool)

Set checkstate of the checkbox.

Parameters:

Name Type Description Default
state CheckStateStr | CheckState | bool

checkstate to use

required
Source code in prettyqt\gui\standarditem.py
def set_checkstate(
    self, state: constants.CheckStateStr | constants.CheckState | bool
):
    """Set checkstate of the checkbox.

    Args:
        state: checkstate to use
    """
    if isinstance(state, bool):
        state = (
            constants.CheckState.Checked if state else constants.CheckState.Unchecked
        )
    self.setCheckState(constants.CHECK_STATE.get_enum_value(state))

set_icon(icon: datatypes.IconType)

Set the icon for the action.

Parameters:

Name Type Description Default
icon IconType

icon to use

required
Source code in prettyqt\gui\standarditem.py
def set_icon(self, icon: datatypes.IconType):
    """Set the icon for the action.

    Args:
        icon: icon to use
    """
    icon = iconprovider.get_icon(icon)
    self.setIcon(icon)

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

Set the alignment of the text.

Parameters:

Name Type Description Default
alignment AlignmentStr | AlignmentFlag

alignment for the format

required
Source code in prettyqt\gui\standarditem.py
def set_text_alignment(
    self, alignment: constants.AlignmentStr | constants.AlignmentFlag
):
    """Set the alignment of the text.

    Args:
        alignment: alignment for the format
    """
    self.setTextAlignment(constants.ALIGNMENTS.get_enum_value(alignment))