Unsolved Delay Loop until a QPushButton pressed
-
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:
I want to as much as possible i don't used multi thread in qt.Qt code :
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.
Any suggestions?
Thanks very much
-
Hi @stackprogramer,
as stupid as it sounds, you cannot use loops for that problem.
You need to factor out the content of your loop to a function, and call that function, with one parameter referencing the file and the second parameter, specifying the save or quit action.
Then update the UI for the next file and quit your function, therefore let the Qt event loop do the waiting for user input for you.
Regards
-
@stackprogramer said in Delay Loop until a QPushButton pressed:
Any suggestions?
Put them strings in a stack/vector. When the button is pressed do whatever you want to do and pop from the stack (or the tail of the vector). If your stack's empty there are no more files to process. As @aha_1980 said, looping around is just not the way to work in event-driven environment.
-
@stackprogramer said in Delay Loop until a QPushButton pressed:
ait = true;
while (wait)
;When i used a function for this situation (I defined a flag, function should check this flag and continue recording file or stop it.)
This function should record a file. When user clicks stop button, flag should change to false. and i expect file recording function is finished.....
But with function way other gui signals not work to change flag or stop is recording.
I emphasize i don't want using multi-thread in qt. -
@stackprogramer Can you please write in complete sentences?
-
you can use a QTimer to continously write to a file in chunks without blocking. No need for multithreading.
Regards