Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt draw colorful text

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!

like image 760
Arton Avatar asked Oct 08 '22 17:10

Arton


1 Answers

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.

like image 160
Matt Phillips Avatar answered Oct 12 '22 09:10

Matt Phillips