Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Space disappears after using Qstylesheet in QLabel



  • I want to let label_8 show different colors text. So I used stylesheet to do this. But I found that if stylesheet is used, the space in the string becomes useless.

            self.main_ui.label_8.setText(f'X: {x},   Y: {y} '
                                         f'<font color=red> R: {r}, </font>'
                                         f'<font color=green>      G: {g}, </font>'
                                         f'<font color=blue> B: {b} </font>')
    

    As the following picture shows, the text becomes compact.
    e258fe78-b734-4640-99e3-a8521f526be1-image.png
    Is there a way to adjust the space among different words and at the same time, keep only one QLabel being used.



  • @feiyuhuahuo
    Since you have to use HTML text to put your colours in, you know that multiple spaces in HTML source only produce a single space in display output. I don't know what exactly QSS accepts, but try &nbsp; for an unbreakable space for each space desired, or a <pre> or maybe separate <span>s, or whatever works.


Log in to reply