I post my solution (in the end heavily based on the Stackoverflow one), just in case it helps anybody. Note I used Qt 6 intead of 5, but it should not matter.

Qt Code:
  1. from PyQt6.QtWidgets import *
  2. import sys
  3. from PyQt6 import QtGui
  4. from PyQt6.QtCore import Qt
  5. from PyQt6.QtGui import QTextDocument, QTextCursor
  6. from PyQt6.QtGui import QColor, QPainter, QPainterPath, QBrush, QPen, QPalette, QFont
  7.  
  8. class MyTextDocument(QTextDocument):
  9. def __init__(self, parent, font):
  10. super().__init__()
  11.  
  12. self.parent = parent
  13.  
  14. self.setDefaultFont(font)
  15.  
  16. def drawContents(self, painter):
  17.  
  18. super().drawContents(painter)
  19.  
  20. my_cursor = self.parent.textCursor()
  21. my_char_format = my_cursor.charFormat()
  22.  
  23. my_char_format.setTextOutline(QPen(Qt.GlobalColor.red, 5))
  24. my_cursor.select(QTextCursor.SelectionType.Document)
  25. my_cursor.mergeCharFormat(my_char_format)
  26.  
  27. super().drawContents(painter)
  28.  
  29. my_char_format.setTextOutline(QPen(Qt.GlobalColor.transparent))
  30. my_cursor.mergeCharFormat(my_char_format)
  31.  
  32. super().drawContents(painter)
  33.  
  34.  
  35. class MyTextEdit(QTextEdit):
  36. def __init__(self):
  37. super().__init__()
  38.  
  39. font = self.currentFont()
  40. font.setPointSize(80)
  41. font.setStyleStrategy(QFont.StyleStrategy.PreferAntialias)
  42.  
  43. self.setDocument(MyTextDocument(self, font))
  44.  
  45. self.setText("Does it work?")
  46. self.resize(800, 400)
  47.  
  48. def paintEvent(self, event):
  49. painter = QPainter(self.viewport())
  50.  
  51. super(MyTextEdit, self).paintEvent(event)
  52.  
  53. self.document().drawContents(painter)
  54.  
  55.  
  56. if __name__ == '__main__':
  57. app = QApplication(sys.argv)
  58.  
  59. win = MyTextEdit()
  60. win.show()
  61.  
  62. app.exec_()
To copy to clipboard, switch view to plain text mode 

Result:



Feel free to post if you have something better in mind / any comment!