Detecting a keypress during some heavy operation

Andras Mantia amantia at kde.org
Sat Feb 7 08:24:24 GMT 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks for the hints. processEvents() is too dirty and error-prone. I should 
try the second one, it may help a little.

Andras

On Saturday 07 February 2004 03:25, Marc Mutz wrote:
> On Friday 06 February 2004 14:41, Andras Mantia wrote:
> <snip>
>
> >  I would like to detect if the user pressed the key while the app is
> > doing some heavy operation, so it will be possible to suspend/stop
> > that operation and make the user interface more responsive. Is it
> > possible to do without threads (I'd like to avoid them as they may
> > introduce new problems)? I've tried reimplementing the keyPressEvent
> > in the widget which has the focus (the editor widget), but it seems
> > that it is never called.
>
> <snip>
>
> Event processing needs the event loop. So you have two options:
>
> 1. The dirty one is to periodically call qApp->processEvents(). It's
> dirty since the user might have CTRL-Q'ed your application, so you must
> be prepared that everything you believed in has vanished when that call
> returns (the most surprising of which might be a deleted "this"; there
> is an option to exclude user input, but you _want_ user input). A clean
> way to avoid this shooting into your own foot is to use a modal
> progress dialog and asking it's wasCanceled() method whether to stop.
>
> 2. If the operation can be divided into chunks (say, ten iterations of
> the for loop each), then you can put the operation into a slot, call
> that slot to perform the first chunk and make the slot call itself for
> the next chunk via the event loop by adding a
>   QTimer::singleShot( 0, this, SLOT(mySlot()) )
> just before returning from the slot.
>
> Note that if you perform blocking IO in such an operation, then that
> will prevent your app to respond, too, of course. For that, use
> KIO::Job, or QSocketNotifer, as usual.
>
> Hope that helps.
>
> Marc

- -- 
Quanta Plus developer - http://quanta.sourceforge.net
K Desktop Environment - http://www.kde.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)

iD8DBQFAJKC4TQdfac6L/08RAiJ9AJ4gnjgr3lVReesKtkEeoWhO8OUwfwCgr6qi
Jjvq+Cad4wdvmnbvuD2MTQ4=
=RB+I
-----END PGP SIGNATURE-----




More information about the kde-core-devel mailing list