Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved] Animate QGraphicsWidget by change of position.

    General and Desktop
    3
    7
    2898
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • idlefrog
      idlefrog last edited by

      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???

      1 Reply Last reply Reply Quote 0
      • M
        mcosta last edited by

        Hi,

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

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

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply Reply Quote 0
        • idlefrog
          idlefrog last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • idlefrog
            idlefrog last edited by

            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... :(

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • idlefrog
                idlefrog last edited by

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

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  You're welcome !

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post