[Solved] Animate QGraphicsWidget by change of position.



  • This must be an easy one, but for some reason I can't animate my Widget.

    So a snippet of my widget code is:

    @class RoundedBox : public QGraphicsWidget
    {
    Q_PROPERTY( QPointF pos READ pos WRITE setPos )

    public:

    RoundedBox(QGraphicsWidget *parent = 0);
    
    QPointF pos() const{ return QGraphicsObject::pos(); } // don't really need these - just for debug
    void setPos( QPointF& pos ) { QGraphicsObject::setPos( pos ); }
    

    ...
    };@

    And in my main window
    I have:

    @void MainWindow::animate()
    {

    QPropertyAnimation animation(_rounded_box, "pos"); // just change position
    animation.setDuration(10000);
    animation.setStartValue(QPointF(0, 0));
    animation.setEndValue(QPointF(100, 30));
    
    animation.start();
    

    }@

    Now the RoundedBox widget has been added to the scene, and is visible, but does not move when 'animate' is called.
    Am I doing something rather silly here???



  • Hi,

    are you really defining "pos" property?
    If yes, you're overriding default property.

    Try to remove PROPERTY and methods pos() and setPos().



  • Well, I put that 'pos' property in there just to see if 'setPos' was ever called. Sadly it is never called. But I fail to see why.



  • Following the documentation I even tried to animate this object in the graphics scene:

    @class MyGraphicsRectItem : public QObject, public QGraphicsRectItem
    {
    Q_OBJECT
    Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry)
    Q_PROPERTY(QPointF pos READ pos WRITE setPos)

    public:
    explicit MyGraphicsRectItem(QGraphicsRectItem* parent=0):QGraphicsRectItem(parent){}
    explicit MyGraphicsRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0):QGraphicsRectItem(x,y,w,h,parent){}

    QRectF geometry(){ return rect() ;}
    void setGeometry( QRectF& r){ setRect(r);}
    

    };@

    If I just set the position manually in the 'animate' method the item moves... but the animation itself never happens. And there are no error messages either, such as a missing property, or setter/getter etc... :(


  • Lifetime Qt Champion

    Hi,

    Your QPropertyAnimation goes out of scope at the end of your animate function hence it doesn't run.

    @
    QPropertyAnimation *animation = new QPropertyAnimation(_rounded_box, "pos"); // just change position
    animation->setDuration(10000);
    animation->setStartValue(QPointF(0, 0));
    animation->setEndValue(QPointF(100, 30));

    animation->start(QAbstractAnimation::DeleteWhenStopped);
    

    @

    If you run your animation more than once, consider making it a member of your MainWindow class so you don't have to recreate it each time animate() is called

    Hope it helps



  • Oh yes!...Thank you very much! :) Greatly appreciated :)
    The above code worked :)


  • Lifetime Qt Champion

    You're welcome !

    Please update the thread's title to solved so other people will know that this issue has a solution.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.