Skip to content

TableWidgetItem

Qt Base Class: QTableWidgetItem

Signature: QTableWidgetItem(self, icon: Union[PySide6.QtGui.QIcon, PySide6.QtGui.QPixmap], text: str, type: int = <ItemType.Type: 0>) -> None QTableWidgetItem(self, other: PySide6.QtWidgets.QTableWidgetItem) -> None QTableWidgetItem(self, text: str, type: int = <ItemType.Type: 0>) -> None QTableWidgetItem(self, type: int = <ItemType.Type: 0>) -> None

Base classes

Name Children Inherits
QTableWidgetItem
PySide6.QtWidgets
QTableWidgetItem(self, icon: Union[PySide6.QtGui.QIcon, PySide6.QtGui.QPixmap], text: str, type: int \= <ItemType.Type: 0>) -> None

â‹” Inheritance diagram

graph TD
  1473296347632["widgets.TableWidgetItem"]
  1473290736464["QtWidgets.QTableWidgetItem"]
  1473291690208["Shiboken.Object"]
  140713234304496["builtins.object"]
  1473290736464 --> 1473296347632
  1473291690208 --> 1473290736464
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: QTableWidgetItem

Item for use with the QTableWidget class.

Source code in prettyqt\widgets\tablewidgetitem.py
class TableWidgetItem(QtWidgets.QTableWidgetItem):
    """Item for use with the QTableWidget class."""

    def __setitem__(self, index: int | constants.ItemDataRoleStr, value):
        self.set_data(index, value)

    def __getitem__(self, index: int | constants.ItemDataRoleStr):
        return self.get_data(index)

    def set_flag(
        self, flag_name: constants.ItemFlagStr | constants.ItemFlag, value: bool
    ):
        """Set a flag based on str name."""
        flag = constants.ITEM_FLAG.get_enum_value(flag_name)
        if value:
            self.setFlags(self.flags() | flag)
        else:
            self.setFlags(self.flags() & ~flag)

    def set_editable(self, editable: bool):
        """Set whether this item is user-editable."""
        self.set_flag("editable", editable)

    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):
        """Set checkstate of the checkbox.

        Args:
            state: checkstate to use
        """
        self.setCheckState(constants.CHECK_STATE.get_enum_value(state))

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

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

    def set_text_alignment(
        self,
        horizontal: constants.HorizontalAlignmentStr
        | constants.AlignmentFlag
        | None = None,
        vertical: constants.VerticalAlignmentStr | constants.AlignmentFlag | None = None,
    ):
        """Set text alignment of the checkbox.

        Args:
            horizontal: horizontal text alignment to use
            vertical: vertical text alignment to use
        """
        match horizontal, vertical:
            case None, None:
                return
            case None, _:
                flag = constants.V_ALIGNMENT.get_enum_value(vertical)
            case _, None:
                flag = constants.H_ALIGNMENT.get_enum_value(horizontal)
            case _, _:
                flag = constants.V_ALIGNMENT.get_enum_value(
                    vertical
                ) | constants.H_ALIGNMENT.get_enum_value(horizontal)
        self.setTextAlignment(flag)

    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:
        return None if (icon := self.icon()).isNull() else gui.Icon(icon)

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

    def get_data(self, role: constants.ItemDataRoleStr | int) -> Any:
        if isinstance(role, str):
            role = constants.ITEM_DATA_ROLE[role]
        return super().data(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:
                if isinstance(size, QtCore.QSize):
                    size = (size.width(), size.height())
                tooltip = f'<img src={path!r} width="{size[0]}" height="{size[1]}">'
        if rich_text:
            tooltip = f"<html>{html.escape(tooltip)}</html>"
        super().setToolTip(tooltip)

get_checkstate() -> constants.CheckStateStr

Return checkstate.

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

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

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

Set checkstate of the checkbox.

Parameters:

Name Type Description Default
state CheckStateStr | CheckState

checkstate to use

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

    Args:
        state: checkstate to use
    """
    self.setCheckState(constants.CHECK_STATE.get_enum_value(state))

set_editable(editable: bool)

Set whether this item is user-editable.

Source code in prettyqt\widgets\tablewidgetitem.py
def set_editable(self, editable: bool):
    """Set whether this item is user-editable."""
    self.set_flag("editable", editable)

set_flag(flag_name: constants.ItemFlagStr | constants.ItemFlag, value: bool)

Set a flag based on str name.

Source code in prettyqt\widgets\tablewidgetitem.py
def set_flag(
    self, flag_name: constants.ItemFlagStr | constants.ItemFlag, value: bool
):
    """Set a flag based on str name."""
    flag = constants.ITEM_FLAG.get_enum_value(flag_name)
    if value:
        self.setFlags(self.flags() | flag)
    else:
        self.setFlags(self.flags() & ~flag)

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\widgets\tablewidgetitem.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(horizontal: constants.HorizontalAlignmentStr | constants.AlignmentFlag | None = None, vertical: constants.VerticalAlignmentStr | constants.AlignmentFlag | None = None)

Set text alignment of the checkbox.

Parameters:

Name Type Description Default
horizontal HorizontalAlignmentStr | AlignmentFlag | None

horizontal text alignment to use

None
vertical VerticalAlignmentStr | AlignmentFlag | None

vertical text alignment to use

None
Source code in prettyqt\widgets\tablewidgetitem.py
def set_text_alignment(
    self,
    horizontal: constants.HorizontalAlignmentStr
    | constants.AlignmentFlag
    | None = None,
    vertical: constants.VerticalAlignmentStr | constants.AlignmentFlag | None = None,
):
    """Set text alignment of the checkbox.

    Args:
        horizontal: horizontal text alignment to use
        vertical: vertical text alignment to use
    """
    match horizontal, vertical:
        case None, None:
            return
        case None, _:
            flag = constants.V_ALIGNMENT.get_enum_value(vertical)
        case _, None:
            flag = constants.H_ALIGNMENT.get_enum_value(horizontal)
        case _, _:
            flag = constants.V_ALIGNMENT.get_enum_value(
                vertical
            ) | constants.H_ALIGNMENT.get_enum_value(horizontal)
    self.setTextAlignment(flag)