Skip to content

StarDelegate

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
  1473367022816["itemdelegates.StarDelegate"]
  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 --> 1473367022816
  1473296344704 --> 1473296354464
  1473299815024 --> 1473296344704
  140713234304496 --> 1473299815024
  1473293727280 --> 1473296354464
  1473293758512 --> 1473293727280
  1473288842240 --> 1473293758512
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: StyledItemDelegate

A delegate class that allows us to render our star ratings.

Source code in prettyqt\itemdelegates\stardelegate.py
class StarDelegate(widgets.StyledItemDelegate):
    """A delegate class that allows us to render our star ratings."""

    ID = "star"

    def paint(
        self,
        painter: gui.QPainter,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        star_rating = StarRating(index.data())

        # If the row is currently selected, we need to make sure we
        # paint the background accordingly.
        if option.state & widgets.Style.StateFlag.State_Selected:
            # The original C++ example used option.palette.foreground() to
            # get the brush for painting, but there are a couple of
            # problems with that:
            #   - foreground() is obsolete now, use windowText() instead
            #   - more importantly, windowText() just returns a brush
            #     containing a flat color, where sometimes the style
            #     would have a nice subtle gradient or something.
            # Here we just use the brush of the painter object that's
            # passed in to us, which keeps the row highlighting nice
            # and consistent.
            painter.fillRect(option.rect, painter.brush())

        # Now that we've painted the background, call star_rating.paint()
        # to paint the stars.
        star_rating.paint(painter, option.rect, option.palette)

    def sizeHint(self, option: widgets.QStyleOptionViewItem, index: core.ModelIndex):
        """Return the size needed to display the item in a QSize object."""
        star_rating = StarRating(index.data())
        return star_rating.sizeHint()

    # The next 4 methods handle the custom editing that we need to do.
    # If this were just a display delegate, paint() and sizeHint() would
    # be all we needed.

    def createEditor(
        self,
        parent: widgets.QWidget,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        """Create and return the StarEditor object we'll use to edit the StarRating."""
        editor = StarEditor(parent)
        editor.editing_finished.connect(self.commitAndCloseEditor)
        return editor

    def setEditorData(self, editor: StarEditor, index: core.ModelIndex):
        """Set the data to be displayed and edited by our custom editor."""
        editor.set_star_rating(index.data())

    def setModelData(
        self,
        editor: widgets.QWidget,
        model: core.QAbstractItemModel,
        index: core.ModelIndex,
    ):
        """Get the data from our custom editor and stuffs it into the model."""
        model.setData(index, editor.star_rating.star_count)

    def commitAndCloseEditor(self):
        editor = self.sender()

        # The commitData signal must be emitted when we've finished editing
        # and need to write our changed back to the model.
        self.commitData.emit(editor)
        self.closeEditor.emit(editor, self.EndEditHint.NoHint)

createEditor(parent: widgets.QWidget, option: widgets.QStyleOptionViewItem, index: core.ModelIndex)

Create and return the StarEditor object we'll use to edit the StarRating.

Source code in prettyqt\itemdelegates\stardelegate.py
def createEditor(
    self,
    parent: widgets.QWidget,
    option: widgets.QStyleOptionViewItem,
    index: core.ModelIndex,
):
    """Create and return the StarEditor object we'll use to edit the StarRating."""
    editor = StarEditor(parent)
    editor.editing_finished.connect(self.commitAndCloseEditor)
    return editor

setEditorData(editor: StarEditor, index: core.ModelIndex)

Set the data to be displayed and edited by our custom editor.

Source code in prettyqt\itemdelegates\stardelegate.py
def setEditorData(self, editor: StarEditor, index: core.ModelIndex):
    """Set the data to be displayed and edited by our custom editor."""
    editor.set_star_rating(index.data())

setModelData(editor: widgets.QWidget, model: core.QAbstractItemModel, index: core.ModelIndex)

Get the data from our custom editor and stuffs it into the model.

Source code in prettyqt\itemdelegates\stardelegate.py
def setModelData(
    self,
    editor: widgets.QWidget,
    model: core.QAbstractItemModel,
    index: core.ModelIndex,
):
    """Get the data from our custom editor and stuffs it into the model."""
    model.setData(index, editor.star_rating.star_count)

sizeHint(option: widgets.QStyleOptionViewItem, index: core.ModelIndex)

Return the size needed to display the item in a QSize object.

Source code in prettyqt\itemdelegates\stardelegate.py
def sizeHint(self, option: widgets.QStyleOptionViewItem, index: core.ModelIndex):
    """Return the size needed to display the item in a QSize object."""
    star_rating = StarRating(index.data())
    return star_rating.sizeHint()

⌗ Property table

Qt Property Type Doc
objectName QString

Delegate ID: star