PDA

View Full Version : Add a QGraphicsTextItem with a gradient



harmodrew
16th November 2010, 10:38
Hello!

I'd like to add a text with a gradient like this
http://doc.qt.nokia.com/4.0/qt4-arthur.html#pen-and-brush-transformation

But I'm using a QGraphicsScene (and it doesn't have a setPen() method) and I should add a QGraphicsTextItem...is there a way to do so?

Thanks

Lykurg
16th November 2010, 10:52
One way is to subclass QGraphicsTextItem and do the painting yourself. Depending on how complicated your text actually is.

harmodrew
16th November 2010, 11:02
I'd like to build up a gradient system for a general text (for example it should work for both "Hello World!" and "Last week I went in Canada and met John. Then...").
How can I do the painting myself? I'm not sure about the functions I have to use to make the painting...

thanks

Lykurg
16th November 2010, 11:05
The question is more if you plan to use HTML within your item. As for normal texts, you have to reimp the paint method and use the painter. Then you could set the gradient to the pen. See QGraphicsItem::paint(). You can also try to just set the pen and call the base class implementation with the modified painter.

harmodrew
16th November 2010, 11:09
I'll try it and let you know...thanks!

harmodrew
16th November 2010, 17:50
I've solved in a very simple way (but it's not the more general-purpose way). I used this site
http://www.tektek.org/color/
to generate a HTML text with a "gradient" color. so I call the setHTML() method of the qGraphicsTextItems passing the HTML strings..:)