Skip to content

IconDelegate

Qt Base Class: QStyledItemDelegate

Signature: QStyledItemDelegate(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
StyledItemDelegate
prettyqt.widgets.styleditemdelegate

⋔ Inheritance diagram

graph TD
  1473367021840["itemdelegates.IconDelegate"]
  1473296354464["widgets.StyledItemDelegate"]
  1473296344704["widgets.AbstractItemDelegateMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473293727280["QtWidgets.QStyledItemDelegate"]
  1473293758512["QtWidgets.QAbstractItemDelegate"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473296354464 --> 1473367021840
  1473296344704 --> 1473296354464
  1473299815024 --> 1473296344704
  140713234304496 --> 1473299815024
  1473293727280 --> 1473296354464
  1473293758512 --> 1473293727280
  1473288842240 --> 1473293758512
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: StyledItemDelegate

Delegate to paint QIcons, QPixmaps, QColors and QImages.

Source code in prettyqt\itemdelegates\icondelegate.py
class IconDelegate(widgets.StyledItemDelegate):
    """Delegate to paint QIcons, QPixmaps, QColors and QImages."""

    ID = "icon"

    def __init__(self, role: constants.ItemDataRole = constants.USER_ROLE, **kwargs):
        self._role = role
        self.margin = 10
        super().__init__(**kwargs)

    def paint(
        self,
        painter: gui.QPainter,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        """Override to paint an icon based on given Pixmap / Color / Icon.

        Pixmap / Color / Icon must be set to '_role'

        Args:
            painter (gui.QPainter): painter to paint the icon
            option (widgets.QStyleOptionViewItem): state of the item to be displayed
            index (core.ModelIndex): index which gets decorated
        """
        super().paint(painter, option, index)
        value = index.data(self._role)
        if not value:
            return
        mode = gui.Icon.Mode.Normal

        if not option.state & widgets.Style.StateFlag.State_Enabled:
            mode = gui.Icon.Mode.Disabled
        elif option.state & widgets.Style.StateFlag.State_Selected:
            mode = gui.Icon.Mode.Selected
        match value:
            case gui.QPixmap():
                icon = gui.QIcon(value)
                option.decorationSize = int(value.size() / value.devicePixelRatio())

            case gui.QColor():
                pixmap = gui.QPixmap(option.decorationSize)
                pixmap.fill(value)
                icon = gui.QIcon(pixmap)

            case gui.QImage():
                icon = gui.QIcon(gui.QPixmap.fromImage(value))
                option.decorationSize = int(value.size() / value.devicePixelRatio())

            case gui.QIcon():
                icon = value
                is_on = option.state & widgets.Style.StateFlag.State_Open
                state = gui.Icon.State.On if is_on else gui.Icon.State.Off
                actual_size = option.icon.actualSize(option.decorationSize, mode, state)
                option.decorationSize.boundedTo(actual_size)
            case _:
                raise ValueError(value)
        r = core.Rect(core.Point(), option.decorationSize)
        r.moveCenter(option.rect.center())
        r.setRight(option.rect.right() - self.margin)
        state = (
            gui.Icon.State.On
            if option.state & widgets.Style.StateFlag.State_Open
            else gui.Icon.State.Off
        )
        alignment = constants.ALIGN_RIGHT | constants.ALIGN_V_CENTER
        icon.paint(painter, r, alignment, mode, state)

paint(painter: gui.QPainter, option: widgets.QStyleOptionViewItem, index: core.ModelIndex)

Override to paint an icon based on given Pixmap / Color / Icon.

Pixmap / Color / Icon must be set to '_role'

Parameters:

Name Type Description Default
painter QPainter

painter to paint the icon

required
option QStyleOptionViewItem

state of the item to be displayed

required
index ModelIndex

index which gets decorated

required
Source code in prettyqt\itemdelegates\icondelegate.py
def paint(
    self,
    painter: gui.QPainter,
    option: widgets.QStyleOptionViewItem,
    index: core.ModelIndex,
):
    """Override to paint an icon based on given Pixmap / Color / Icon.

    Pixmap / Color / Icon must be set to '_role'

    Args:
        painter (gui.QPainter): painter to paint the icon
        option (widgets.QStyleOptionViewItem): state of the item to be displayed
        index (core.ModelIndex): index which gets decorated
    """
    super().paint(painter, option, index)
    value = index.data(self._role)
    if not value:
        return
    mode = gui.Icon.Mode.Normal

    if not option.state & widgets.Style.StateFlag.State_Enabled:
        mode = gui.Icon.Mode.Disabled
    elif option.state & widgets.Style.StateFlag.State_Selected:
        mode = gui.Icon.Mode.Selected
    match value:
        case gui.QPixmap():
            icon = gui.QIcon(value)
            option.decorationSize = int(value.size() / value.devicePixelRatio())

        case gui.QColor():
            pixmap = gui.QPixmap(option.decorationSize)
            pixmap.fill(value)
            icon = gui.QIcon(pixmap)

        case gui.QImage():
            icon = gui.QIcon(gui.QPixmap.fromImage(value))
            option.decorationSize = int(value.size() / value.devicePixelRatio())

        case gui.QIcon():
            icon = value
            is_on = option.state & widgets.Style.StateFlag.State_Open
            state = gui.Icon.State.On if is_on else gui.Icon.State.Off
            actual_size = option.icon.actualSize(option.decorationSize, mode, state)
            option.decorationSize.boundedTo(actual_size)
        case _:
            raise ValueError(value)
    r = core.Rect(core.Point(), option.decorationSize)
    r.moveCenter(option.rect.center())
    r.setRight(option.rect.right() - self.margin)
    state = (
        gui.Icon.State.On
        if option.state & widgets.Style.StateFlag.State_Open
        else gui.Icon.State.Off
    )
    alignment = constants.ALIGN_RIGHT | constants.ALIGN_V_CENTER
    icon.paint(painter, r, alignment, mode, state)

⌗ Property table

Qt Property Type Doc
objectName QString

Delegate ID: icon