Reputation: 654
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_animation = new QPropertyAnimation(ui->label, "geometry");
startAnimation();
}
MainWindow::~MainWindow()
{
delete ui;
delete m_animation;
}
void MainWindow::startAnimation()
{
m_animation->setDuration(3000);
m_animation->setKeyValueAt(0, QRect(50, 50, 128, 128));
m_animation->setKeyValueAt(0.25, QRect(200, 50, 128, 128));
m_animation->setKeyValueAt(0.5, QRect(50, 50, 128, 128));
m_animation->setKeyValueAt(0.75, QRect(200, 50, 128, 128));
m_animation->setKeyValueAt(1, QRect(50, 50, 128, 128));
m_animation->start();
}
I want to show the animation done in startAnimation
function multiple times, right now it shows only two times. I tried using QSequential
but that is also not working. I also tried to connect this function with a thread but when I am creating object of thread class, it is showing unresolved symbol error.
Upvotes: 1
Views: 1230
Reputation: 6735
You should set the loopCount property.
void MainWindow::startAnimation()
{
m_animation->setLoopCount(10);
m_animation->setDuration(3000);
m_animation->setKeyValueAt(0, QRect(50, 50, 128, 128));
m_animation->setKeyValueAt(0.25, QRect(200, 50, 128, 128));
m_animation->setKeyValueAt(0.5, QRect(50, 50, 128, 128));
m_animation->setKeyValueAt(0.75, QRect(200, 50, 128, 128));
m_animation->setKeyValueAt(1, QRect(50, 50, 128, 128));
m_animation->start();
}
Upvotes: 1
Reputation: 18504
No, you don't need thread here. All what you need is QSequentialAnimationGroup
(as I said earlier). For example:
int numberOfLoops = 3;
QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);
for(int i = 0; i < numberOfLoops; ++i)
{
QPropertyAnimation *m_animation = new QPropertyAnimation(this, "geometry");
m_animation->setDuration(3000);
m_animation->setKeyValueAt(0, QRect(50, 50, 128, 128));
m_animation->setKeyValueAt(0.25, QRect(200, 50, 128, 128));
m_animation->setKeyValueAt(0.5, QRect(50, 50, 128, 128));
m_animation->setKeyValueAt(0.75, QRect(200, 50, 128, 128));
m_animation->setKeyValueAt(1, QRect(50, 50, 128, 128));
group->addAnimation(m_animation);
}
group->start(QAbstractAnimation::DeleteWhenStopped);
But I still think that you code now is a little wrong for my approach, so final code is:
//type 3 and animation will repeat 3 time, type 10 and it will repeat 10 and so on
int numberOfLoops = 5;
QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);
for(int i = 0; i < numberOfLoops; ++i)
{
QPropertyAnimation *m_animation = new QPropertyAnimation(this, "geometry");
m_animation->setDuration(1500);
m_animation->setKeyValueAt(0, QRect(50, 50, 128, 128));
m_animation->setKeyValueAt(0.5, QRect(200, 50, 128, 128));
m_animation->setKeyValueAt(1, QRect(50, 50, 128, 128));
group->addAnimation(m_animation);
}
group->start(QAbstractAnimation::DeleteWhenStopped);
Also don't think that this code produce memory leaks, no! As you can see, I added QAbstractAnimation::DeleteWhenStopped
flag which means that all your created QPropertyAnimation
objects will be destroyed too(when animation will be stoped)! To prove that you can add one line and run app, you will see that all objects will be deleted (but it is just a proof, you don't need do this in release
app for example):
//...
group->addAnimation(m_animation);
connect(m_animation,&QPropertyAnimation::destroyed,[](){qDebug()<< "m_animation destroyed";});
//...
But I used here C++11
(CONFIG += c++11
to .pro
file) and new syntax of signals and slots, but of course you can use old syntax if you want.
Upvotes: 0