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.
{
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*/
};
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*/
};
To copy to clipboard, switch view to plain text mode
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
}
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
}
To copy to clipboard, switch view to plain text mode
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();
QPropertyAnimation animation(item0, "pos");
animation.setDuration(1000);
animation.setStartValue(QPointF(-200,0));
animation.setEndValue(QPointF(-400,0));
animation.start();
To copy to clipboard, switch view to plain text mode
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
Bookmarks