Question

I use QPainter to drawText on QPrinter; however, there is a problem. I hope I can draw a colorful text. For example, drawing "Hello World" where the "H" is green, "ello" is yellow, "World" is blue.
For the time being, I call drawText 3 times and using different QPen to change color. Moreover, I must calculate the position where to draw at 3 times. Finally, I couldn't align a string in a QRect because I don't draw a complete sentence. Is there a way to solve it with a simple way? Any hint is appreciated! Thanks!

Was it helpful?

Solution

You can do this by writing html right into your text string, including CSS specifications for color.

Html in widget text

CSS within the html Use the html style tag to access this.

Edit: To get this on a QPaintDevice, use QPainter::drawStaticText(). It works just like drawText but reads rich text by default. You need to #include <QStaticText> to use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top