2014-01-10 21 views
2

我陷入了場景中簡單Propertyanimation的屬性。該項目只需移動一段距離。隨着財產「pos」它會移動。與我的財產「ScenePosition」它不起作用。調試器進入函數setScenePosition(),但它對顯示的場景沒有影響。QProperty動畫Qt中的scenePos

// chip.h 
class Chip : public QObject, public QGraphicsEllipseItem 
{ 
    Q_OBJECT 
    //THIS WORKS - But i have to use scenePosition instead because of the scene 
    Q_PROPERTY(QPointF pos READ pos WRITE setPos) 

    // MY ATTEMPT 
    Q_PROPERTY(QPointF ScenePosition READ ScenePosition WRITE setScenePosition) 

public: 
    explicit Chip(int x, int y, int w, int h, QObject *parent=NULL); 


    void setScenePosition(QPointF p); 
    QPointF ScenePosition(); 

}; 

我想我以錯誤的方式使用了scenePos()。

//chip.cpp 
    void Chip::setScenePosition(QPointF p) 
    { 
     this->scenePos().setX(p.x()); 
     this->scenePos().setY(p.y()); 
    } 

最後調用動畫。這個調用看起來沒什麼問題,因爲它可以與 新的QPropertyAnimation(item,「pos」)配合使用,但與「ScenePosition」不能配合使用,這讓我對setter的實現有疑問。

 item = new Chip(col*wCol, 100, wCol, wCol); 
     item->setVisible(true); 
     QPropertyAnimation *animation = new QPropertyAnimation(item, "ScenePosition"); 
     addItem(item); 

     animation->setDuration(1000); 
     animation->setEndValue(QPoint(col*wCol, 400 - wCol*stacked)); 
     animation->setEasingCurve(QEasingCurve::OutElastic); 
     animation->start(); 

回答

2

this->scenePos()返回QPointF。使用​​隻影響暫時的QPointF對象,顯然不能影響項目位置。你需要使用類似this->setScenePos(p);的東西。但是,沒有這樣的方法。如果該項目沒有父項目,則this->setPos(p)將等同工作。如果該項目有父項目,則可以使用this->setPos(this->parentItem()->mapfromScene(p));mapFromScene會將p從場景座標轉換爲父項目座標系,並且使用父項目座標系定位子項目,因此它應該可以正常工作。