Details
-
Bug
-
Resolution: Out of scope
-
Not Evaluated
-
None
-
6.4.0
-
None
-
Windows 11 Dev Build
Python: 3.11.0
PySide: 6.4.1
Description
I wrote my own delegator for QStandardItem. It doesn't work as it should at all. I've already asked in the forum, but they can't help me either. Example code below. I have attached a video that shows the problem.
from PySide6.QtWidgets import QApplication, QMainWindow, QStyledItemDelegate, QStyle, QStyleOptionViewItem, QTreeView from PySide6.QtCore import QModelIndex from PySide6.QtGui import QBrush, QStandardItem, QStandardItemModel, QPainter, QPainterPath, QPen, QFont import sys DISPLAY_ROLE = 0 BACKGROUND_ROLE = 8 class StyledItemDelegate(QStyledItemDelegate): def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex): # painter.setRenderHint(QPainter.Antialiasing) path = QPainterPath() path.addRoundedRect(option.rect, 5, 5) painter.fillPath(path, index.data(BACKGROUND_ROLE)) painter.setPen(QPen(QBrush("black"), 2)) painter.setFont(QFont("Segoe UI", 9, QFont.Normal)) rect = option.rect rect.setX(rect.x() + 5) painter.drawText(rect, index.data(DISPLAY_ROLE)) painter.setPen(QPen(QBrush("black"), 1)) if QStyle.State_HasFocus & option.state: painter.drawPath(path) class MainWindow(QMainWindow): def __init__(self): super().__init__() treeview = QTreeView() self.setCentralWidget(treeview) model = QStandardItemModel() treeview.setModel(model) treeview.setItemDelegate(StyledItemDelegate()) treeview.header().setVisible(False) _ = QStandardItem("my item") model.appendRow(_) model.setData(_.index(), QBrush("#ffffff"), role=BACKGROUND_ROLE) _ = QStandardItem("my item") model.appendRow(_) model.setData(_.index(), QBrush("#ffffff"), role=BACKGROUND_ROLE) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())