PDA

View Full Version : Animation and QGraphics



PrimeCP
7th May 2009, 00:55
I'm using the graphics objects to display boxes with arrows between them. I want animation on those arrows/lines that starts at one end and goes to the other.

The animation works great when the start and end locations of the arrow don't change during QTimeLine execution, i.e. I leave the view static.
But since the user can drag the boxes around (and thus I redraw the arrows to follow) I need to reset the start and end position inside the QGraphicsItemAnimation so the animation matches the new arrow line.

I tried to update the start and end locations in the paint method of the Arrow object but that just makes the animation go crazy (it flips randomly all over the place). I assume it's because I changed the animation positions when the QTimeLine object was in the RUNNING state.



void Arrow::paint(QPainter *painter,
const QStyleOptionGraphicsItem *, QWidget *)
{
...
///update the arrow to have a new source and destination, draw arrow, etc
...

if (mAnimate)
{
mAnimationObj->setPosAt(0, mSourcePoint);
mAnimationObj->setPosAt(1, mDestPoint);
}
}



How should I dynamically change the QGraphicsItemAnimation start and end positions (0 and 1)?

For the end product, I want the user to be able to drag the boxes around, have the arrows follow (which currently works) as well as have the animation continue along the new arrow route.

If that isn't possible, then I need to figure out some way to capture the dragging and stop the arrow animation until the user releases and then I guess reset it...?

Thanks

wysota
7th May 2009, 01:02
QGraphicsItemAnimation::clear() the animation, optionally stop the timeline and restart it after setting new properties for the animation.

PrimeCP
7th May 2009, 01:31
Adding the "clear" before resetting the positions in "paint" worked perfectly, thanks.