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!
Asked
Active
Viewed 2,905 times
5

Arton
- 443
- 1
- 6
- 15
1 Answers
4
You can do this by writing html right into your text string, including CSS specifications for color.
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.

Matt Phillips
- 9,465
- 8
- 44
- 75
-
I draw something on QPrinter because I wanna make a pdf file. Could rich text be used by QPainter? – Arton Feb 10 '12 at 17:35
-
@Anton See my edit, I've confirmed that this works in my own code. – Matt Phillips Feb 10 '12 at 18:05