A QTextDocument is never alone. You ought to draw it in a paint function. There, you pass a context parameter. The latter might be just a QAbstractTextDocumentLayout.PaintContext(). It has a palette attribute. There, you set the brush.

That's how it might look like:
Qt Code:
  1. class StyledItemDelegate(QStyledItemDelegate):
  2. def paint(
  3. self,
  4. painter: QPainter,
  5. ) -> None:
  6. self.initStyleOption(option, index)
  7. style: QStyle
  8. if option.widget:
  9. style = option.widget.style()
  10. else:
  11. style = QApplication.style()
  12. doc.setHtml(option.text)
  13. option.text = ""
  14. style.drawControl(QStyle.ControlElement.CE_ItemViewItem, option, painter)
  15. ctx: QAbstractTextDocumentLayout.PaintContext = QAbstractTextDocumentLayout.PaintContext()
  16. text_rect: QRect = style.subElementRect(QStyle.SubElement.SE_ItemViewItemText, option)
  17. painter.save()
  18.  
  19. if option.state & QStyle.StateFlag.State_Selected:
  20. ctx.palette.setBrush(QPalette.ColorRole.Text, option.palette.highlightedText())
  21.  
  22. painter.translate(text_rect.topLeft())
  23. painter.setClipRect(option.rect.translated(-text_rect.topLeft()))
  24. painter.translate(0, 0.5 * (option.rect.height() - doc.size().height()))
  25. doc.documentLayout().draw(painter, ctx)
  26. painter.restore()
To copy to clipboard, switch view to plain text mode 

Punctual as always, me.