[Solved] How to "pause" FOR loop when executing a "QPropertyAnimation"
-
I'm stucked at a problem that may be simple or not, i don't know yet as my head is filled with thoughts but none of them is working as intended - i don't know if i'm just too newbie at this or whatever.
Here is my problem..
I have 5 buttons that trigger animations - each time i press one button "a ball" cross the room and go to the position of that button. So the ball is initially at 0. If i press button 2, the ball goes to 2 (animated with QPropertyAnimation, in 2 secs). If i press 4, the ball goes to 4The problem is, if i press 2 and 4 almost at the same time, i want the ball to go to 2, and then after a while go to 4. Initially i was calling an animation on each "click", and the ball was only going to position 4, ignoring the 2. My workaround was to create an array with 5 positions.
@int positions[5] = {0}@
So with each click i'll set that "position" to 1. Then at runtime, if the user clicked on 2 and 4 at the same time i'd have this:
[0,1,0,1,0,0];And finally i'd run a FOR loop like this
@for(int i=0;i<=5;i++){
if(positions[i]==1) {
//callAnimation
}
}@to check for which positions my ball must travel.
Problem is. The FOR loop is being ran in question of ms - calling the animation to position 2 and then calling the animation to position 4 on top of that. As output i can only see the ball travelling to position 4, ALTHOUGH on the console i can see that the animation is being called to both positions 2 and 4.
What i would like to do? When i enter my IF for the first time (//callAnimation), i'd want to PAUSE the for loop for a while, or something like that - and run my first set of animation calmly. Then, after the ball reached position 2, i'd like to continue with the for loop (and it will enter the if at position 4 and run the animation of it). That way the user will be able to see the 2 animations. (that can be 3,4,5,etc, according to how many buttons the user pressed in a short while)
The pause(); function just won't work here, 'cause it will pause everything, even my animation - i just want the loop to stop for a while...
If any of you have ANY thoughts about how to solve this, please share.... Sorry for my english and thanks in advance
-
Hello,
I suggest to use queue. When button is clicked, it's number( for example ) is added to queue if there is another animation running. When animation finishes, you check the queue, and if it is not empty, start next animation and so on until all animations are played.
-
task_struct is right: use a form of a queue. You should realize that an animation is an asynchronous process. It takes time, and multiple redraws will happen in between. That requires the event loop to be running. Using a for loop won't allow that to work properly, as you found out. So, instead of 'pausing' your for loop, you should just add a new target to your queue, and connect the finished signal of your animation to a slot that will trigger the next animation from the queue.
-
Exactly what i was looking for! Thanks to both of you!
I've never used queue, but it was pretty simple adapt it to my code!
I was with the feeling that the for loop wasn't my best option... that's why i've explained my problem a little further..
Now i just have to adjust some minor things here and there. Thanks again!
-
Be sure and edit the title of the thread to add [Solved].
Thanks!