Results 1 to 3 of 3

Thread: multiple QPropertyAnimations after each other, how???

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default multiple QPropertyAnimations after each other, how???

    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?

    Qt Code:
    1. void Block::test() { // slot, called when pressed button
    2. for (int i=0; i < 5; i++) {
    3. stepRight();
    4.  
    5. if (!finished)
    6. printf("anim not finished\n");
    7. }
    8. }
    9.  
    10. void Block::stepRight(){
    11. if( !finished) return; // if I remove this only last animation is played
    12. animatePosition(QPointF(pos().x(),pos().y()), QPointF(pos().x()+width, pos().y()));
    13. setX(x() + width);
    14. }
    15.  
    16. void Block::animatePosition(const QPointF& start, const QPointF& end){
    17. anim = new QPropertyAnimation(this, "pos");
    18. finished = false;
    19. anim->setDuration(3000);
    20. anim->setStartValue(start);
    21. anim->setEndValue(end);
    22.  
    23. QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
    24.  
    25. anim->start();
    26. }
    27.  
    28. void Block::animationFinished() {
    29. // This slot is called when animation ends
    30.  
    31. printf("anim finished\n");
    32. finished = true;
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: multiple QPropertyAnimations after each other, how???

    Have a look at QSequentialAnimationGroup.

  3. #3
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: multiple QPropertyAnimations after each other, how???

    Thanks... btw maybe it was a bit of a stupid question.

    However now I have another issue/question regarding this QSequentialAnimationGroup thing... I want to adjust the x value after the block (a QGraphicsItem as said) has moved to the right. Just to keep track of it and for interaction between classes.

    But if I adjust the x value as in the example below the animation is not longer displayed correctly.
    The block is then first displayed on it's FINAL position, and then moved back to it's original position for the animation to start. But the block should not be first displayed on it's final position.

    Qt Code:
    1. Block * block1 = new Block(40,20,20); // params: x pos, y pos, width
    2. // using setPos(x,y) in constructor
    3.  
    4. // declaration of 2 qpropertyanimations for block1
    5. QPropertyAnimation * anim4 = new QPropertyAnimation(block1, "pos");
    6. anim4->setDuration(2000);
    7. anim4->setStartValue(QPointF(40,20));
    8. anim4->setEndValue(QPointF(60,20));
    9. QPropertyAnimation * anim5 = new QPropertyAnimation(block1, "pos");
    10. anim5->setDuration(2000);
    11. anim5->setStartValue(QPointF(60,20));
    12. anim5->setEndValue(QPointF(80,20));
    13.  
    14. QSequentialAnimationGroup * group = new QSequentialAnimationGroup;
    15. printf("x: %f\n", block1->x()); // says 40
    16. group->addAnimation(anim4);
    17. block1->setX(block1->x()+20);
    18. printf("x: %f\n", block1->x()); // says 60
    19. group->addAnimation(anim5);
    20. block1->setX(block1->x()+20);
    21. printf("x: %f\n", block1->x()); // says 80
    22. group->start();
    To copy to clipboard, switch view to plain text mode 

    I tried to change the order of the setX(..) commands but it does not help
    Why is it first displayed on position 80? Any ideas?
    If I do NOT add the setX(..) command. the animation works properly, but then the x() value of block1 remains 40.

Similar Threads

  1. how to use Multiple timers?
    By qtlinuxnewbie in forum Newbie
    Replies: 8
    Last Post: 21st September 2012, 09:01
  2. Multiple displays on X11
    By alisami in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2009, 17:25
  3. QPropertyAnimations in Qt Script
    By Scorp2us in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2009, 14:48
  4. multiple defination
    By phillip_Qt in forum Qt Programming
    Replies: 4
    Last Post: 13th December 2007, 17:32
  5. Replies: 0
    Last Post: 21st December 2006, 11:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.