By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

QTimer and PyQt

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a


Adrian Casey wrote:
[...]
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.

Jul 18 '05 #3

P: n/a
Jim
Phil Thompson wrote:
On Tuesday 20 July 2004 12:41 pm, Adrian Casey wrote:
2. At timeout, check if any fields have changed (using the isChanged()
method.) and if so, reset the timer.
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

Jul 18 '05 #4

P: n/a
Jim wrote:
Phil Thompson wrote:
On Tuesday 20 July 2004 12:41 pm, Adrian Casey wrote:
2. At timeout, check if any fields have changed (using the isChanged()
method.) and if so, reset the timer.

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


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.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.