2013-11-01 27 views
0

我有一堆QRects和QGraphicsScene中的一些文本,我試圖用QPropertyAnimation進行動畫製作。動畫文字工作正常,但QRects不工作,因爲他們無法轉換爲QGraphicsObject用QPropertyAnimation動畫化QRect

這工作完全

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); 

但是,這不,因爲項目[2 * 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); 

有沒有辦法解決這個問題?

+1

QRect不是一個圖形項目,你的意思是QGraphicsRectItem? –

+0

恩,也許吧。我添加了與QGraphicsScene :: addRect – Ben

回答

0

toGraphicsObject返回空指針,因爲QGraphicsRectItem不是QGraphicsObject。您不能使用QGraphicsRectItem執行動畫。我可以建議兩種解決方法:

  1. 創建自己的類從QObjectQGraphicsRectItem衍生,打造「POS」屬性並執行其getter和setter。看一個例子here
  2. 創建自己的類從QGraphicsObject派生。實施其boundingRectpaint純虛擬方法,使其畫一個矩形。
+0

rects謝謝,我通過製作一個自定義的QGraphicsObject工作。 – Ben