Search code examples
pythonpython-3.xqtpyqt5

PyQt5 - Can I use different font sizes on different parts of the text in a label?


I would like to have a label where I have text with parts where the sizes are different. Is this possible with PyQt5 or some CSS?

PS: I haven't coded it and am more looking for the theoretical answer than code so I can't provide any code or such.


Solution

  • QLabel supports a limited HTML subset, and basic styling is still enabled:

    self.label.setText('''
        normalText 
        <span style="font-size: 18px">biggerText</span>
        <span style="font-size: 24px">veryBigText</span>
    ''')
    

    Read more about the Supported HTML Subset, which is valid for any rich-text enabled widgets (QLabel and all widgets that use a QTextDocument, like all QTextEdit descendants).