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. How to create a Animation pause (solved)

How to create a Animation pause (solved)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.9k Views
  • 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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by houmingc
    #1

    How to create a pause at location 2000 before moving to 4000.

    
                    (2000,870,30,30)
                    (0,870,30,30)  -> (4000,870,30,30)
    
            moviemoving = new QMovie(":/name/cut2.png");
            gifmoving->setMovie(moviemoving);
            moviemoving->start();
            QTimer *timer = new QTimer();
            QObject::connect(timer,SIGNAL(timeout()),this,SLOT(move()));
            timer->start(5000);
    
        void MainWindow::move()
       {
             animation = new QPropertyAnimation(gifmoving,"geometry"); 
             //QPropertyAnimation* animation in header
             animation->setDuration(8000);
             animation->setLoopCount(1);
             animation->setStartValue(QRect(0,870,30,30));
             animation->setEndValue(QRect(4000,870,30,30));
             animation->start(QAbstractAnimation::DeleteWhenStopped);
        }
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      You can use setKeyValueAt.
      For example if the animation duration is 8s and you want a 2s pause in the middle you can do

      animation->setStartValue(QRect(0,870,30,30));
      animation->setEndValue(QRect(4000,870,30,30));
      
      // 0.375 = 3s/8s  which is position of the pause start
      animation->setKeyValueAt(0.375, QRect(2000,870,30,30));
      // 0.625 = 5s/8s  which is position of the pause end
      animation->setKeyValueAt(0.625, QRect(2000,870,30,30));
      

      One thing caught my eye so I wanted to ask - the timer will fire every 5s and the animation duration is 8s so you will have increasing number of overlapping animations after few seconds fighting for the same property.
      Also I know this is just a fragment of the code but make sure you delete the timer at some point as the snippet above leaks it.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        houmingc
        wrote on last edited by Chris Kawa
        #3

        Chris i still can't get it to work

         QPropertyAnimation* animation;   //header file
        
            QTimer *timer1 = new QTimer();
            QObject::connect(timer1,SIGNAL(timeout()),this,SLOT(move()));
            timer1->start(5000);
        
        void MainWindow::move()
        {
                animation = new QPropertyAnimation();
                animation->setLoopCount(1);
                animation->start();
                animation->setStartValue(QRect(0,20,90,30));
                animation->setEndValue(QRect(2000,20,90,30));
        
                animation->setKeyValueAt(0.375, QRect(1000,20,90,30));
                animation->setKeyValueAt(0.625, QRect(1000,20,90,30));
                this->timer1->stop();
         
        }
        
        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          You didn't set the target, the property name and the duration on the animation. Start the animation after you set it up (not required but makes more sense for the reader). You don't need to keep animation pointer around. Use signal/slots to manage its lifetime. You are leaking memory (timer1 and animation instances). The timer fires after 5s (is that intended?). If you want to start the animation only once then use QTimer::singleShot static method.

          So more or less:

          //where you want to start the animation:
          QTimer::singleShot(5000, this, &MainWindow::move); //this will start the animation after 5s
          
          
          //and the move function:
          void MainWindow::move()
          {
              auto animation = new QPropertyAnimation(gifmoving, "geometry");
              connect(animation, &QPropertyAnimation::finished, animation, &QPropertyAnimation::deleteLater);
          
              animation->setDuration(8000);
              animation->setStartValue(QRect(0,20,90,30));
              animation->setKeyValueAt(0.375, QRect(1000,20,90,30));
              animation->setKeyValueAt(0.625, QRect(1000,20,90,30));
              animation->setEndValue(QRect(2000,20,90,30));
          
              animation->start();
          }
          
          H 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            You didn't set the target, the property name and the duration on the animation. Start the animation after you set it up (not required but makes more sense for the reader). You don't need to keep animation pointer around. Use signal/slots to manage its lifetime. You are leaking memory (timer1 and animation instances). The timer fires after 5s (is that intended?). If you want to start the animation only once then use QTimer::singleShot static method.

            So more or less:

            //where you want to start the animation:
            QTimer::singleShot(5000, this, &MainWindow::move); //this will start the animation after 5s
            
            
            //and the move function:
            void MainWindow::move()
            {
                auto animation = new QPropertyAnimation(gifmoving, "geometry");
                connect(animation, &QPropertyAnimation::finished, animation, &QPropertyAnimation::deleteLater);
            
                animation->setDuration(8000);
                animation->setStartValue(QRect(0,20,90,30));
                animation->setKeyValueAt(0.375, QRect(1000,20,90,30));
                animation->setKeyValueAt(0.625, QRect(1000,20,90,30));
                animation->setEndValue(QRect(2000,20,90,30));
            
                animation->start();
            }
            
            H Offline
            H Offline
            houmingc
            wrote on last edited by houmingc
            #5

            Chris, thanks i am learning alot ..

            auto animation = new QPropertyAnimation(gifmoving, "geometry");
            with auto -> i get error animation does not name a type
            without -> it is running fine

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #6

              There should not be any -> after auto. auto is a c++11 feature that deduces expression type and thus lets you type less. To enable it in your compiler (assuming it supports it) you need to add this line to your .pro file CONFIG += c++11 (except for VS compiler which has this always on).

              If you don't want to use c++11 then just name the whole type manually, i.e.

              QPropertyAnimation* animation = new QPropertyAnimation(gifmoving, "geometry");
              
              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