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!
I use QPainter to drawText on QPrinter; however, there is a problem. I hope
Share
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
styletag to access this.Edit: To get this on a
QPaintDevice, useQPainter::drawStaticText(). It works just likedrawTextbut reads rich text by default. You need to#include <QStaticText>to use it.