How to set text in a QLabel and display '<>' characters?

Using PySide2 (basically PyQt5), I'm trying to setText in a QLabel that has the characters '<' and '>' in the string. But, since these characters are used to modify the font, anything within those two characters disappears. I tried escaping them with a backslash, but it still doesn't seem to work...

How can I get the '<' and '>' characters to show up in the QLabel?

EDIT: here's a bit what my code is doing (just snagged the important parts):

# color and text get set at various places in my code (len_infs is just the length of the
#    influence item list - hopefully that's obvious...)
#
color = '#FF0000'
text = 'Influence count &lt%d&gt exceeds minimum row count...' %len_infs

# status_label is a QLabel that displays the text in the appropriate color within the label
#
status_label.setText('status: <font color=%s>%s</font>' %(color, text))

As you can see, I'm using &lt and &gt in the string based on ekhumoro's suggestion, but I do not get '<' or '>' in the resulting QLabel text

I want it to print status: Influence count <7> exceeds minimum row count...

but it actually prints status: Influence count &lt7&gt exceeds minimum row count...

In both, 'status:' is white, while the rest is red as intended.

NOTE: Using '<%d>' in the string results in status: Influence count exceeds minimum row count...

What else do I need to do to format the string properly?


Solution 1:

The QLabel class automatically supports rich-text, so you need to use character entity references to escape special characters. For <, use &lt; and for >, use &gt;. If you can't edit the text directly, use html.escape to do the conversion.

Alternatively, to turn off rich-text support altogether, you can do this:

label.setTextFormat(QtCore.Qt.PlainText)