PDA

View Full Version : question regards how to use animation framework in graphics view items



favor
1st April 2010, 06:21
I want to animate an graphic item in graphics view, I want to this item moving, rotating around y axis, scaling at the same

time. I want to use graphicsview framework and animation framework.

according to the animation framework manual, if I want to animate the graphic view item, I should inhert from Object, and

declare many metaObject properties for the animation framework.

1, declare a class with many animation properties.

class GraphicItem: public QObject, public QGraphicsRectItem
{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos) //for moving property
Q_PROPERTY(float scaleFactor READ scaleFactor WRITE setScaleFactor) //for scaling
Q_PROPERTY(int rotateAngle READ rotateAngle WRITE setRotateAngle) //for rotating

/*some functions*/

//below is setter and getter funs of the properties.
float scaleFactor() const;
void setScaleFactor(float);
int rotateAngle() const;
void setRotateAngle(int);

private:
/*some variables declaration*/
};

2. getter and setter funcs are like below, and the pos property has pos getter and setPos setter automatically.


float GraphicItem::scaleFactor() const
{
return factor;
}

void GraphicItem::setScaleFactor(float scale)
{
factor = scale;
setScale( scale ); //this can scaling the item
}

int GraphicItem::rotateAngle() const
{
return angle;
}
void GraphicItem::setRotateAngle(int a)
{
angle = a;
setTransform(QTransform().rotate(a, Qt::YAxis)); //can rotating the item around y axis
}

3, use animation framework to animate the item. item0 is a instance of graphicItem object.


QPropertyAnimation animation(item0, "pos");
animation.setDuration(1000);
animation.setStartValue(QPointF(-200,0));
animation.setEndValue(QPointF(-400,0));

animation.start();

I desire it can moving from point (-200,0) to point (-400,0), but it can't moving at all, the same to the other two properites

scaleFactor and rotateAngle.

what's wrong with my code?

please give me some hints, any suggestion is highly appreciated.

thanks;)

aamer4yu
1st April 2010, 06:35
Did you have a look at examples in Qt Demos ?
From 4.6.x onwards you have examples for Animation framework. Check them out... especially the animated tiles example

favor
1st April 2010, 10:43
if "QPropertyAnimation animation(item0, "pos");" is in a function then it is auto destroyed at the function end.

it works! now;)

thanks