class HTMLDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(HTMLDelegate, self).__init__(parent)
def paint(self, painter, option, index):
self.initStyleOption(options, index)
self.doc.setHtml(options.text)
options.text = ""
style.
drawControl(QStyle.
CE_ItemViewItem, options, painter
)
if option.
state & QStyle.
State_Selected: ctx.
palette.
setColor(QPalette.
Text, option.
palette.
color(
textRect
= style.
subElementRect(QStyle.
SE_ItemViewItemText, options
) painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
def sizeHint(self, option, index):
self.initStyleOption(options, index)
self.doc.setDocumentMargin(1)
self.doc.setHtml(options.text)
return QSize(self.
doc.
idealWidth(),
23)
class HTMLDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(HTMLDelegate, self).__init__(parent)
self.doc = QTextDocument(self)
def paint(self, painter, option, index):
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)
style = QApplication.style()
self.doc.setHtml(options.text)
options.text = ""
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.HighlightedText))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
def sizeHint(self, option, index):
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)
self.doc.setDocumentMargin(1)
self.doc.setHtml(options.text)
return QSize(self.doc.idealWidth(), 23)
To copy to clipboard, switch view to plain text mode
Bookmarks