def paint(self, painter, option, index): options = QStyleOptionViewItem(option) row_obj = index.data(Qt.DisplayRole) self.initStyleOption(options, index) # print(option.rect.width()) style = QApplication.style( ) if options.widget is None else options.widget.style() doc = QTextDocument() doc.setHtml(row_obj.to_html()) doc.setTextWidth(option.rect.width() - 10) # doc.setPageSize(300) # print(doc.loadResource(3)) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) gene_obj = index.data(Qt.DisplayRole) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(gene_obj.to_html()) doc.setTextWidth(options.rect.width() - 10) return QSize(doc.idealWidth(), doc.size().height())
def drawContents(self, painter): """ Reimplementation of drawContents to limit the drawing inside `textRext`. """ painter.setPen(self.__color) painter.setFont(self.font()) if self.__textRect: rect = self.__textRect else: rect = self.rect().adjusted(5, 5, -5, -5) tformat = self.__textFormat if tformat == Qt.AutoText: if mightBeRichText(self.__message): tformat = Qt.RichText else: tformat = Qt.PlainText if tformat == Qt.RichText: doc = QTextDocument() doc.setHtml(self.__message) doc.setTextWidth(rect.width()) cursor = QTextCursor(doc) cursor.select(QTextCursor.Document) fmt = QTextBlockFormat() fmt.setAlignment(self.__alignment) cursor.mergeBlockFormat(fmt) painter.save() painter.translate(rect.topLeft()) doc.drawContents(painter) painter.restore() else: painter.drawText(rect, self.__alignment, self.__message)