PDA

View Full Version : Delay Loop until a QPushButton pressed



ljshap
15th January 2009, 16:36
I want to iterate through a list of files and have the program wait until a user presses the skip or save push button before going to the next file. I created a boolean variable called wait which would be set to true and changed to false when a push button is pressed. The following is a code extract showing the logic:


for (start=0 ; start<numberOfFiles ; start++) // main Processing Loop
{ ...
wait = true;
while (wait)
; // Good For Nothing
...
}

// Assume QPushButton clicked SIGNAL is properly linked to savedPressed() SLOT.

void BYDATE::savePressed () // SLOT
wait=false;
}


The above code causes the program to hang. Is it possible to change the wait variable when the program is in the while loop. I'm hoping to avoid multi-threading, if possible. I tried getchar(), but that didn't work either.

Any suggestions?

Thanks

ljshap
15th January 2009, 17:45
I found one solution, which was to put :


QApplication::processEvents();

inside the while loop.

This solution seems to be working and is much easier than using the QEventLoop class.

wysota
16th January 2009, 09:19
Yeah, and it's also wrong ;)

http://doc.trolltech.com/qq/qq27-responsive-guis.html

ljshap
16th January 2009, 13:40
Yeah, and it's also wrong ;)


I didn't say it was the right solution, I only said it worked, which it does. :rolleyes:
I'll take a closer look at your link, when I get a chance.

Thanks

wysota
16th January 2009, 14:30
It worked for now. Try running two such loops at the same time. Using QEventLoop is much better, especially that it's much easier to timeout such call and resources are used wisely.

ljshap
17th January 2009, 13:17
It worked for now. Try running two such loops at the same time. Using QEventLoop is much better, especially that it's much easier to timeout such call and resources are used wisely.

I hope you don't think I was disagreeing with you! Your points are well taken. What APPEARS to work now may cause problems down the road or in other situations, so why not do it right to begin with.

Thanks Again

wysota
17th January 2009, 16:30
I'm more against the "it's much easier than using QEventLoop" statement than about agreeing or not about some solution being proper (or not).