PyQt, but probably should not play that big of a role.

So, what I want is to have listview that looks and acts exactly like the default listview, but that does support html tags in the text.

There are several stackoverflow questions and from all the answers I rustled up something that comes pretty close, but would like someone who actually understands delegates to look at it, if I am not doing something wrong, choosing wrong approach.

Here is the most basic one that got me pretty close to what I want.
Pls make note that theres no use of custom sizeHint(), only paint(), also theres no ues setClipRect() since it caused issues on KDE and when removed everything seems fine.



code of it:

Qt Code:
  1. class HTMLDelegate(QStyledItemDelegate):
  2. def __init__(self, parent=None):
  3. super(HTMLDelegate, self).__init__(parent)
  4. self.doc = QTextDocument(self)
  5.  
  6. def paint(self, painter, option, index):
  7. painter.save()
  8.  
  9. options = QStyleOptionViewItem(option)
  10. self.initStyleOption(options, index)
  11.  
  12. self.doc.setHtml(options.text)
  13. options.text = ""
  14.  
  15. style = QApplication.style() if options.widget is None \
  16. else options.widget.style()
  17. style.drawControl(QStyle.CE_ItemViewItem, options, painter)
  18.  
  19. ctx = QAbstractTextDocumentLayout.PaintContext()
  20.  
  21. if option.state & QStyle.State_Selected:
  22. ctx.palette.setColor(QPalette.Text, option.palette.color(
  23. QPalette.Active, QPalette.HighlightedText))
  24.  
  25. textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
  26. painter.translate(textRect.topLeft())
  27. self.doc.documentLayout().draw(painter, ctx)
  28.  
  29. painter.restore()
To copy to clipboard, switch view to plain text mode 

Now, the problem is that a selection highlighted seems to be somehow cut, not done right.
The way I find to fix it was to set document margin to 0 or 1 (default is 4), but that now moves the items all the way to the left, touching the left line.

Qt Code:
  1. self.doc.setDocumentMargin(0)
To copy to clipboard, switch view to plain text mode 



Ok, so now to solve that minor visual degradation, I learned that I can set the items starting points and after adding this code in to paint() the listview looks the same as default

Qt Code:
  1. rect = options.rect
  2. options.rect = QRect(rect.x()+3, rect.y(), rect.width(), rect.height())
To copy to clipboard, switch view to plain text mode 



And so this is the final code, but I feel like it could have go with margin 4 and not moving the items by few pixels right, only if I knew how to fix that highlight selection...
Also I am kinda worried that theres something needed that might cause it act wonky even if its alright on my system...

Qt Code:
  1. class HTMLDelegate(QStyledItemDelegate):
  2. def __init__(self, parent=None):
  3. super(HTMLDelegate, self).__init__(parent)
  4. self.doc = QTextDocument(self)
  5. self.doc.setDocumentMargin(0)
  6.  
  7. def paint(self, painter, option, index):
  8. painter.save()
  9.  
  10. options = QStyleOptionViewItem(option)
  11. self.initStyleOption(options, index)
  12.  
  13. self.doc.setHtml(options.text)
  14. options.text = ""
  15.  
  16. rect = options.rect
  17. options.rect = QRect(rect.x()+3, rect.y(), rect.width(), rect.height())
  18.  
  19. style = QApplication.style() if options.widget is None \
  20. else options.widget.style()
  21. style.drawControl(QStyle.CE_ItemViewItem, options, painter)
  22.  
  23. ctx = QAbstractTextDocumentLayout.PaintContext()
  24.  
  25. if option.state & QStyle.State_Selected:
  26. ctx.palette.setColor(QPalette.Text, option.palette.color(
  27. QPalette.Active, QPalette.HighlightedText))
  28.  
  29. textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
  30. painter.translate(textRect.topLeft())
  31. self.doc.documentLayout().draw(painter, ctx)
  32.  
  33. painter.restore()
To copy to clipboard, switch view to plain text mode