Qt draw colorful text

I am using QPainter to drawText on QPrinter; however there is a problem. I hope I can draw colorful text. For example, the picture “Hello World”, where “H” is green, “ello” is yellow, and “World” is blue.
I am currently calling drawText 3 times and using different QPen to change the color. Moreover, I have to calculate the position where I need to draw 3 times. Finally, I could not align the string in QRect because I am not making a complete sentence. Is there a way to solve this in a simple way? Any hint is appreciated! Thank you

+5
source share
1 answer

You can do this by writing html directly in the text line, including CSS specifications for color.

Html in widget text

CSS html html style, .

: QPaintDevice, QPainter::drawStaticText(). , drawText, -. #include <QStaticText> .

+4

All Articles