我想做一个graphicitem的动画,包含一个图片的绕Y轴旋转,移动,放大的效果,图片是放在graphicsitem里面。 我想对这个item使用animation动画。 但是看了animation的文档后,感觉animation动画是改变meta-object property产生动画效果的,如果要对grahpicitem用animation,在定义这个item类的时候,就必须继承QObject和使用Q_OBJECT宏,以及使用Q_PROPERTY定义meta-object属性。
我是这么做的:
1, 定义一个graphicitem类,继承QObject,和使用Q_OBJECT宏,使用Q_PROPERTY定义属性
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. meta-object属性setter和getter函数的定义.
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,使用animation类产生动画效果,这里只产生移动的效果。
QPropertyAnimation animation(item0, "pos");
animation.setDuration(1000);
animation.setStartValue(QPointF(-200,0));
animation.setEndValue(QPointF(-400,0));
animation.start();
我原本设想这样可以让这个item从point (-200,0)移动到point (-400,0), 但是运行结果没有任何动画。使用到其他两个定义的属性上面(scaleFactor,rotateAngle)也是一样的结果。
请问这事怎么回事呀,我写得代码哪儿有问题吗?
谢谢了
thanks;)