QTimer and PyQt

A

Adrian Casey

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?

Cheers.
Adrian.
 
P

Phil Thompson

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
 
G

Gabriel Cooper

Adrian said:
[...]
2. At timeout, check if any fields have changed (using the isChanged()
method.) and if so, reset the timer.
Wouldn't this defeat the purpose of your login? e.g. I log in to your
app, edit something, step away for 3 hours, bob comes in, clicks OK on
the edited dialog and voila, still validated in the system.

Why not simply pop up a password box when the OK button is pressed if
the user is no longer logged in?

As in:
User starts app and validates himself via a password
User edits a record (or whatever)
User stops typing for 3 minutes.
You let the timer expire.
User clicks OK.
User has no access, so pop up a password dialog appears to re-validate him.
 
J

Jim

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

Most input widgets emit a signal when their value changes ("textChanged",
"currentChanged", "valueChanged", "clicked", etc). You could connect all of
the signals from the widgets to a single slot which resets the timer.

See both the Qt and PyQt docs on signals and slots.

I liked the suggestion about revalidating the user when the user presses
"ok" better though.

Jim
 
A

Adrian Casey

Jim said:
Most input widgets emit a signal when their value changes ("textChanged",
"currentChanged", "valueChanged", "clicked", etc). You could connect all
of the signals from the widgets to a single slot which resets the timer.

See both the Qt and PyQt docs on signals and slots.

I liked the suggestion about revalidating the user when the user presses
"ok" better though.

Jim

So do I Jim. I settled on Phil's suggestion and implemented an eventFilter
for each QLineEdit object (using queryList("QLineEdit")) and the
installEventFilter method. Works like a champ!

Essentially, the eventFilter checks the events registered by each field and,
if they are keystrokes, a self.timer.changeinterval(120000) is done to
restart the timer with a 2 minute timeout.

Thanks again for your responses.

Regards.
Adrian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top