PySide6 Snippets

These snippets may apply to PySide2 also but are untested there.

Plain Text to Rich Text Conversion

Handling special characters in Qt Rich Text Widget.

If you are using a rich text widget, then any special characters, like say the less than sign <, if written to the text object will cause your output to be broken. Qt provides a function for these cases: convertFromPlainText

from PySide6.QtGui import Qt
...
    def my_method(plain_text)
        myQmlModel.rich_text_property = Qt.convertFromPlainText(plain_text)

Carriage Returns and Line Feeds in Rich Text do not work with WordWrap

In addition to the convertFromPlainText method, you can not have line feeds in your text, so you need to find and convert those to <br/>. If you do not, WordWrap option will not work correctly and your lines will extend past your widget width. At least in PySide 6.2.3.

Reference: QtGui — Qt for Python


Qt ® is a registered trademark of The Qt Company Ltd. and its subsidiaries.

Python ® is a registered trademark of the Python Software Foundation.