PDA

View Full Version : How to render part of a QPainterPath?



Gnurou
9th October 2009, 02:36
Hello everybody,

I'm trying to render animations in which a complex QPainterPath (essentially made of curves) is drawn step-by-step on the screen. One can think of it as a "manual handwriting" effect.

In order to perform that effect correctly, I'd need to be able to draw only part of the path. However, I cannot find any method in the API or combination of methods that would allow me to do that. Would someone have a suggestion on that topic?

Gnurou
18th October 2009, 14:23
Just for the record, I have been able to solve this problem using a dash pattern. Following is an example code, where dLength is the amount of the painterPath we want to draw:



QPen painterPen(painter->pen());
QPen pen(painterPen);
QVector<qreal> dashes;
// The first element of the vector is the length we want to draw
// We must divide it by the width of the pen because
// the dash painting works with pen units.
// The second element is the empty part of the path - setting it
// to the length of the path ensures the dash pattern won't repeat.
dashes << dLength / pen.width() << painterPath.length();
pen.setDashPattern(dashes);
painter->setPen(pen);
painter->drawPath(painterPath);
painter->setPen(painterPen);


It works good and fast. And of course, it is possible to draw from the middle of the path by using 4 values in the vector instead of 2.