e1in9
1st August 2010, 15:58
Hi,
I have made a class (Block) that inherits from QGraphicsItem and QObject and therefore can be animated by QPropertyAnimation. Currently I have implemented that the Block-object moves & animates to the right by 20 position-units. (See function stepRight() below). This works when called one time. But my problem is, I can not get it to play multiple times after each other, e.g. in a loop.
For example when I call the function test() below it starts the first animation, and then immediately the others, while the first is not finished yet with playing. So... only the first animation is executed/played!
I want to be able to call stepRight() five times in a loop and that all the five animations get executed sequentially. Is there any sort of method for such thing, must I use other animation methods? Timers?
void Block::test() { // slot, called when pressed button
for (int i=0; i < 5; i++) {
stepRight();
if (!finished)
printf("anim not finished\n");
}
}
void Block::stepRight(){
if( !finished) return; // if I remove this only last animation is played
animatePosition(QPointF(pos().x(),pos().y()), QPointF(pos().x()+width, pos().y()));
setX(x() + width);
}
void Block::animatePosition(const QPointF& start, const QPointF& end){
anim = new QPropertyAnimation(this, "pos");
finished = false;
anim->setDuration(3000);
anim->setStartValue(start);
anim->setEndValue(end);
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
anim->start();
}
void Block::animationFinished() {
// This slot is called when animation ends
printf("anim finished\n");
finished = true;
}
I have made a class (Block) that inherits from QGraphicsItem and QObject and therefore can be animated by QPropertyAnimation. Currently I have implemented that the Block-object moves & animates to the right by 20 position-units. (See function stepRight() below). This works when called one time. But my problem is, I can not get it to play multiple times after each other, e.g. in a loop.
For example when I call the function test() below it starts the first animation, and then immediately the others, while the first is not finished yet with playing. So... only the first animation is executed/played!
I want to be able to call stepRight() five times in a loop and that all the five animations get executed sequentially. Is there any sort of method for such thing, must I use other animation methods? Timers?
void Block::test() { // slot, called when pressed button
for (int i=0; i < 5; i++) {
stepRight();
if (!finished)
printf("anim not finished\n");
}
}
void Block::stepRight(){
if( !finished) return; // if I remove this only last animation is played
animatePosition(QPointF(pos().x(),pos().y()), QPointF(pos().x()+width, pos().y()));
setX(x() + width);
}
void Block::animatePosition(const QPointF& start, const QPointF& end){
anim = new QPropertyAnimation(this, "pos");
finished = false;
anim->setDuration(3000);
anim->setStartValue(start);
anim->setEndValue(end);
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
anim->start();
}
void Block::animationFinished() {
// This slot is called when animation ends
printf("anim finished\n");
finished = true;
}