Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Animate QGraphicsWidget by change of position.
Forum Updated to NodeBB v4.3 + New Features

[Solved] Animate QGraphicsWidget by change of position.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.3k Views 1 Watching
  • 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.
  • idlefrogI Offline
    idlefrogI Offline
    idlefrog
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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
      0
      • idlefrogI Offline
        idlefrogI Offline
        idlefrog
        wrote on last edited by
        #3

        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
        0
        • idlefrogI Offline
          idlefrogI Offline
          idlefrog
          wrote on last edited by
          #4

          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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            0
            • idlefrogI Offline
              idlefrogI Offline
              idlefrog
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved