سؤال

I got stuck with a property for a simple Propertyanimation in a scene. The item just has to move a distance. With the property "pos" it will move. With my Property "ScenePosition" it does not work. The debugger steps into the function setScenePosition() but it has no effect on the displayed scene.

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

};

I guess that i am using the scenePos() in a wrong way.

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

Finally the call of the animation. The call seems to be alright, because it works with new QPropertyAnimation(item, "pos") but not with "ScenePosition" wich puts my implementation of the setter in a doubtful light.

        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();
هل كانت مفيدة؟

المحلول

this->scenePos() returns QPointF. Using this->scenePos().setX(p.x()); only affects temporary QPointF object and obviously cannot affect item position. You would need to use something like this->setScenePos(p);. However, there is no such method. If the item has no parent item, this->setPos(p) will work equivalently. If the item has a parent item, you can use this->setPos(this->parentItem()->mapfromScene(p));. mapFromScene will convert p from scene coordinates to the parent item coordinate system, and child items are positioned using the parent item coordinate system, so it should work correctly.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top