How to create a Animation pause (solved)
-
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); }
-
You can use setKeyValueAt.
For example if the animation duration is 8s and you want a 2s pause in the middle you can doanimation->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. -
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(); }
-
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
andanimation
instances). The timer fires after 5s (is that intended?). If you want to start the animation only once then useQTimer::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(); }
-
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
andanimation
instances). The timer fires after 5s (is that intended?). If you want to start the animation only once then useQTimer::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(); }
-
There should not be any
->
afterauto
.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 fileCONFIG += 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");