我有一堆QRects和QGraphicsScene中的一些文本,我正试图用QPropertyAnimation来制作动画。动画文本运行良好,但QRects无法工作,因为它们无法转换为QGraphicsObject
这工作得很完美
QPropertyAnimation *a = new QPropertyAnimation(this);
a->setTargetObject(items[size.x()*size.y()-1-aa]->toGraphicsObject()); //text
a->setPropertyName("pos");
a->setDuration(animationLength);
a->setStartValue(items[size.x()*size.y()-1-aa]->pos());
a->setEndValue(newTextPos);
a->setEasingCurve(easingCurve);
a->start(QAbstractAnimation::DeleteWhenStopped);但这不是,因为items2*size.x()*size.y()-2-aa->toGraphicsObject()返回一个空指针。
QPropertyAnimation *a = new QPropertyAnimation(this);
a->setTargetObject(items[2*size.x()*size.y()-2-aa]->toGraphicsObject()); //rect
a->setPropertyName("pos");
a->setDuration(animationLength);
a->setStartValue(items[2*size.x()*size.y()-2-aa]->pos());
a->setEndValue(newRectPos);
a->setEasingCurve(easingCurve);
a->start(QAbstractAnimation::DeleteWhenStopped);有办法解决这个问题吗?
发布于 2013-11-02 12:27:08
toGraphicsObject返回空指针,因为QGraphicsRectItem不是QGraphicsObject。不能使用QGraphicsRectItem执行动画。我可以提出两种解决办法:
QObject和QGraphicsRectItem派生的类,创建"pos“属性并实现其getter和setter。参见一个示例这里。QGraphicsObject派生的自己的类。实现它的boundingRect和paint纯虚拟方法,使其绘制一个矩形。https://stackoverflow.com/questions/19732294
复制相似问题