PDA

View Full Version : QGraphicsItemAnimation and QTimeLine



babu198649
28th December 2007, 08:56
hi
the following code works .
but how does the for loop is executed after timer has started(after the for loop the timer is started)


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 20, 20);

QTimeLine *timer = new QTimeLine(5000);
timer->setFrameRange(0, 100);

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(ball);
animation->setTimeLine(timer);

for (int i = 0; i < 200; ++i)
animation->setPosAt(i / 200.0, QPointF(i, i));

QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 250, 250);
scene->addItem(ball);

QGraphicsView *view = new QGraphicsView(scene);
view->show();

timer->start();
return a.exec();
}

jpn
28th December 2007, 10:25
What do you mean is later executed? The loop initializes how QGraphicsItemAnimation will perform after the QTimeLine is started.

babu198649
29th December 2007, 09:31
What do you mean is later executed?

how does the control goes back to for loop(line 15) after timer has started (line 25) .

marcel
29th December 2007, 09:41
how does the control goes back to for loop(line 15) after timer has started (line 25) .
You got it all wrong.
The control will not be passed back to the for loop.



for (int i = 0; i < 200; ++i)
animation->setPosAt(i / 200.0, QPointF(i, i));

All this code does is to store 200 positions inside the animation object.
Once the timer is started, the ball graphics item will be moved sequentially at the positions you previously stored inside the animation object. This will give you an impression of animation.
So that's pretty much how it works.

babu198649
29th December 2007, 11:05
thanks
when the value of the step in the setPosAt ( qreal step, const QPointF & point ) function is smaller(ie.nearer to zero) the animation occurs faster(i.e duration of animation is less) and when the value of step is set to bigger value (nearer to or equal to one ) , the animation animates slowly .

then why do we need the QTimeLine when we can control the duration of animation using setPosAt ( qreal step, const QPointF & point ).

jpn
29th December 2007, 11:24
when the value of the step in the setPosAt ( qreal step, const QPointF & point ) function is smaller(ie.nearer to zero) the animation occurs faster(i.e duration of animation is less) and when the value of step is set to bigger value (nearer to or equal to one ) , the animation animates slowly .
QTimeLine::curveShape


then why do we need the QTimeLine when we can control the duration of animation using setPosAt ( qreal step, const QPointF & point ).
These are positions at given step. QTimeLine controls the duration and speed of the animation.

ashukla
29th December 2007, 11:25
thanks
when the value of the step in the setPosAt ( qreal step, const QPointF & point ) function is smaller(ie.nearer to zero) the animation occurs faster(i.e duration of animation is less) and when the value of step is set to bigger value (nearer to or equal to one ) , the animation animates slowly .

then why do we need the QTimeLine when we can control the duration of animation using setPosAt ( qreal step, const QPointF & point ).
For one pass of complete animation QTimeLine is used. It is a time (in millisec)for one pass of animation of object.

babu198649
29th December 2007, 11:45
hi

QTimeLine controls the duration and speed of the animation.

step can decrease the duration of the animation.

Does step decides the sequence in which the item has to be moved .

Gopala Krishna
29th December 2007, 14:16
Basically timeline works on interpolation (http://en.wikipedia.org/wiki/Interpolation) and extrapolation (http://en.wikipedia.org/wiki/Extrapolation) concepts.

It is like the animation's total path is represented as a function f(x) where x is time.
Then we provide 'f(x)' values for our own set of 'x' values.
For example, f(x) should represent (10,10) when x = 5ms
f(x) should represent (15, 15) when x = 10 ms.
That is, each 5ms, 10ms is time step.

Based on these values the f(x) is determined generally. Now a timer is started to emit signals at regular intervals and based on this time, the f(x) is calculated. Setting the item's position to value returned by f(x) at those specific intervals called by signal creates a feeling of animation. This is what QGraphicsAnimation does.

P.S:
I might be wrong. If so please correct me as these concepts are new even to me.

babu198649
31st December 2007, 09:45
thanks Gopala Krishna

After experimenting i now understood that the animation will occur through out the duration(which is set using QTimeLine) only when the step value is set to both 0 &1.

otherwise,
if the step value is set to greater than zero and to 1 then, the animation gets started after a delay.

e.x
animation->setPosAt(0.5,QPointF(0,0));
animation->setPosAt(1, QPointF(0,550)) //the animation will start after a delay;

similarly, if the step value is set to zero and less than one then, the animation ends before the time to which it has set.


ex.
animation->setPosAt(0.00,QPointF(0,0));
animation->setPosAt(0.80, QPointF(0,550)); //animation ends earlier than the time set.


the problem is when there are multiple points to be set for an animation ,the speed of the animation differs based on the difference between the step values of two points.

how to animate an item at constant speed .

jpn
31st December 2007, 10:11
Steps lie between 0.0 and 1.0 where 0.0 is at the beginning of the animation and 1.0 is in the end.


animation->setPosAt(0.0, QPointF(0,0)); // animation will move the item to (0,0) at step 0.0 (beginning)
animation->setPosAt(0.25, QPointF(1,1)); // animation will move the item to (1,1) at step 0.25 (25% completed)
animation->setPosAt(0.5, QPointF(2,2)); // animation will move the item to (2,2) at step 0.5 (midway)
animation->setPosAt(0.75, QPointF(3,3)); // animation will move the item to (3,3) at step 0.75 (75% completed)
animation->setPosAt(1.0, QPointF(4,4)); // animation will move the item to (4,4) at step 1.0 (end)

How long it will take to reach these steps is up to the attached QTimeLine. GraphicsItemAnimation does not care when these steps are reached. One can even set a certain step by hand via QGraphicsItemAnimation::setStep().

babu198649
31st December 2007, 10:20
thank u jpn
how to animate a sub-classed widget .

jpn
31st December 2007, 10:25
how to animate a sub-classed widget .
Sorry, but how is this question related to QGraphicsItemAnimation, the subject of this thread? QGraphicsItemAnimation handles QGraphicsItems, not QWidgets.

babu198649
31st December 2007, 11:34
sorry
i thought all the animation should be done with QGraphicsItemAnimation.