Skip to content

ButtonDelegate

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

🛈 DocStrings

Bases: StyledItemDelegate

Delegate to show a button inside a table cell.

Source code in prettyqt\itemdelegates\buttondelegate.py
class ButtonDelegate(widgets.StyledItemDelegate):
    """Delegate to show a button inside a table cell."""

    ID = "button"

    def __init__(
        self,
        parent: widgets.QAbstractItemView,
        role: constants.ItemDataRole = constants.USER_ROLE,
    ):
        super().__init__(parent)
        parent.setMouseTracking(True)
        self.btn = widgets.PushButton(parent=parent)
        self.method_role = role
        self.btn.hide()
        self.current_edited_index = core.ModelIndex()
        parent.entered.connect(self.cell_entered)

    #     parent.viewport().installEventFilter(self)

    # def eventFilter(self, source, event) -> bool:
    #     if event.type() == event.Type.MouseMove:
    #         return True
    #     return super().eventFilter(source, event)

    def updateEditorGeometry(
        self,
        editor: widgets.QWidget,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        editor.setGeometry(option.rect)

    def createEditor(
        self,
        parent: widgets.QWidget,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        btn_callback = index.data(self.method_role)
        if btn_callback is None:
            return
        return widgets.PushButton(parent=parent, text=index.data(), clicked=btn_callback)

    def setEditorData(self, editor: widgets.QWidget, index: core.ModelIndex):
        pass
        # editor.setProperty("test", "aa")
        # editor.setText(index.data())

    def setModelData(
        self,
        editor: widgets.QWidget,
        model: core.QAbstractItemModel,
        index: core.ModelIndex,
    ):
        pass
        # model.setData(index, editor.property("test"))

    def cell_entered(self, index: core.ModelIndex):
        # index = self.parent().indexFromItem(item)
        parent: widgets.QAbstractItemView = self.parent()  # type: ignore
        if parent.isPersistentEditorOpen(index):
            parent.closePersistentEditor(self.current_edited_index)
        if parent.itemDelegateForIndex(index) is self:
            # if index.data(self.method_role) is not None:
            parent.openPersistentEditor(index)
            parent.setCurrentIndex(index)
        self.current_edited_index = index

    def paint(
        self,
        painter: gui.QPainter,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        if index.data(self.method_role) is None:
            super().paint(painter, option, index)
            return
        self.btn.setGeometry(option.rect)
        self.btn.setText(index.data())
        if option.state & widgets.Style.StateFlag.State_MouseOver:
            painter.fillRect(option.rect, option.palette.highlight())
        if option.state & widgets.Style.StateFlag.State_Selected:
            painter.fillRect(option.rect, option.palette.highlight())
        pixmap = self.btn.grab()
        painter.drawPixmap(option.rect, pixmap)

⌗ Property table

Qt Property Type Doc
objectName QString

Delegate ID: button