class StyledItemDelegate(QStyledItemDelegate):
def paint(
self,
) -> None:
self.initStyleOption(option, index)
if option.widget:
style = option.widget.style()
else:
doc.setHtml(option.text)
option.text = ""
style.
drawControl(QStyle.
ControlElement.
CE_ItemViewItem, option, painter
) text_rect
: QRect = style.
subElementRect(QStyle.
SubElement.
SE_ItemViewItemText, option
) painter.save()
if option.
state & QStyle.
StateFlag.
State_Selected: ctx.
palette.
setBrush(QPalette.
ColorRole.
Text, option.
palette.
highlightedText())
painter.translate(text_rect.topLeft())
painter.setClipRect(option.rect.translated(-text_rect.topLeft()))
painter.translate(0, 0.5 * (option.rect.height() - doc.size().height()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
class StyledItemDelegate(QStyledItemDelegate):
def paint(
self,
painter: QPainter,
option: QStyleOptionViewItem,
index: QModelIndex | QPersistentModelIndex,
) -> None:
self.initStyleOption(option, index)
style: QStyle
if option.widget:
style = option.widget.style()
else:
style = QApplication.style()
doc: QTextDocument = QTextDocument()
doc.setHtml(option.text)
option.text = ""
style.drawControl(QStyle.ControlElement.CE_ItemViewItem, option, painter)
ctx: QAbstractTextDocumentLayout.PaintContext = QAbstractTextDocumentLayout.PaintContext()
text_rect: QRect = style.subElementRect(QStyle.SubElement.SE_ItemViewItemText, option)
painter.save()
if option.state & QStyle.StateFlag.State_Selected:
ctx.palette.setBrush(QPalette.ColorRole.Text, option.palette.highlightedText())
painter.translate(text_rect.topLeft())
painter.setClipRect(option.rect.translated(-text_rect.topLeft()))
painter.translate(0, 0.5 * (option.rect.height() - doc.size().height()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
To copy to clipboard, switch view to plain text mode
Bookmarks