[Qt-interest] How to implement a `Stop Button' ?

Andre Somers andre at familiesomers.nl
Tue May 11 10:18:27 CEST 2010


On 11-5-2010 10:11, Aaron Lewis wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
> 	I tried to implement a function with QPushButton , that will stop a
> task that is running.
>
> 	e.g A function used to search for specify strings in all of the files
> inside a directory , if the directory has a long scope , user may want
> to stop it.
>
> 	So far what i did is make the QPushButton checkable , and during the
> loop in the directory , everytime before i search in a file , i will
> check if the Button is `checked'.
> 	If it's checked , jump out of the loop immediately , and stop searching.
>
>
> 	The problem is , during the search function , user interface will stop
> answering me , so i won't be able to click on the button , in that case
> , i can't stop the search function.
>
> 	Does anyone has any ideas ?
>
>    

The first thing to do when you have a (potentially) long operation, to 
make it non-blocking on your UI.
There are many ways to do that, the main two ways include using a worker 
thread or cutting up your problem in little chuncks and using a single 
shot 0 ms timer to process the next part of the operation. Personally, I 
would stear clear of using QCoreApplication::processEvents().

Once you have your operation running non-blocking, you can also create a 
functional stop button.

André



More information about the Qt-interest-old mailing list