Question

I am trying to set a font colour for Html text in a created QTextBrowser. I've used basic Html Commands to set paragraphs, change font size etc. but when it comes to setting font colour, it doesn't seem to work?

The code that I used is shown below:

self.key = QtGui.QTextBrowser(self)
        self.key.setHtml(
            """<h1>Key</h1>
            <body>
            <font colour="red">
            GREEN = Overall Progress is 80% or above
            YELLOW = Overall Progress between 65%-79%
            Orange = Overall Progress is 64% or below
            </font>
            </body>"""
            )

It makes Key a header (Bold and Enlarged) via the use of <h1> but using colour tags or evem colour codes (e.g.#00ff00) don't seem to work

Était-ce utile?

La solution

As noted in the comments, the correct property is named color not colour, with that in mind, I'd do away with the font element completely, as its long since deprecated and change your code to, e.g:

self.key = QtGui.QTextBrowser(self)
        self.key.setHtml(
            """<body>
            <h1>Key</h1>
            <div style='color:red;'>
            GREEN = Overall Progress is 80% or above
            YELLOW = Overall Progress between 65%-79%
            Orange = Overall Progress is 64% or below
            </div>
            </body>"""
            )

Even better would be to use an external stylesheet to move your CSS out of being inline, and then apply a class to the div. In addition, all elements should reside within the body tags, so you should also move your h1 below body

With that in mind, I'm not familiar with QTextBrowser however.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top