QTimer and PyQt

Phil Thompson phil at riverbankcomputing.co.uk
Tue Jul 20 08:17:08 EDT 2004


On Tuesday 20 July 2004 12:41 pm, Adrian Casey wrote:
> I'm using a QTimer object to expire certain password protected GUI options
> in my application after 2 minutes.  Currently, the timer is reset each time
> the user presses the 'OK' button.  This is not ideal.  A user may spend 2
> minutes entering data into the GUI before pressing OK in which case the
> timer expires before they have pressed OK.
>
> I want the timer to timeout after 2 minutes of keyboard inactivity (i.e. no
> events sent) instead of 2 minutes following the last 'OK' click.
>
> I can imagine 2 ways to do this -:
>
> 1.  Reset the timer (timer.stop(), timer.start()) each time a keystroke
> event is received
>   -- or --
> 2.  At timeout, check if any fields have changed (using the isChanged()
> method.) and if so, reset the timer.
>
> The problem is, I don't know how to do either in PyQt!  I think option 2
> would be more efficient.  Is it possible to check the parent widget for any
> changes to child widgets or do I have to iterate over each widget on the
> form, checking each indivually?

Note that these questions are Qt questions, not PyQt questions. You may get 
better answers from a Qt mailing list.

For 1., you can detect a keystroke by reimplementing an object's event() 
method. A better solution would be to sub-class from QTimer and use it as an 
event filter (see QObject.installEventFilter()) for all the fields you want 
to watch for keystrokes - then restart the timer when you see a relevant 
keystroke event.

For 2., you would have to iterate over each widget.

Check out the section "Events and Event Filters" in the Qt documentation.

Phil



More information about the Python-list mailing list