def sizeHint(self, option, index): st = QStaticText(index.data(RENDER_ROLE)) st.prepare(font=self.parent().font()) width = max(option.rect.width(), self.parent().width() - 50) if width and width != st.textWidth(): st.setTextWidth(width) br = st.size() return QSize(br.width() + self.MARGIN, br.height() + self.MARGIN)
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) pal = option.palette color = pal.color(QPalette.ColorRole.HighlightedText if option.state & QStyle.StateFlag.State_Selected else QPalette.ColorRole.Text).name() text = '<div style="color:%s">%s</div>' % (color, index.data(RENDER_ROLE)) st = QStaticText(text) st.setTextWidth(option.rect.width()) painter.drawStaticText(option.rect.left() + self.MARGIN // 2, option.rect.top() + self.MARGIN // 2, st)