Detecting a keypress during some heavy operation

Marc Mutz Marc.Mutz at uni-bielefeld.de
Sat Feb 7 01:25:56 GMT 2004


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

-- 
If privacy is outlawed, only outlaws will have privacy.
                                                    -- Phil Zimmermann
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20040207/948b0ea9/attachment.sig>


More information about the kde-core-devel mailing list