def paint(self, painter, option, index): options = QStyleOptionViewItemV4(option) self.initStyleOption(options, index) # Choose appropriate style style = QApplication.style( ) if options.widget is None else options.widget.style() # Convert text into HTML doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(option.rect.width()) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): """Overrided. Paint content.""" text = index.data(Qt.DisplayRole) style = QApplication.style( ) if option.styleObject is None else option.styleObject.style() self._setupTextDocument(text, option.rect.width()) option.text = "" option.backgroundBrush = index.data( Qt.BackgroundRole) or option.backgroundBrush style.drawControl(QStyle.CE_ItemViewItem, option, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) self.textDocument.documentLayout().draw(painter, ctx) painter.restore()