Bear
Bear

Reputation: 5152

QT Event Problem

I am writing a qt program and have the following requirement.

When 30 sec passed without any click, lock the screen. If someone clicks again after these 30 secs, redirect him to a login screen.

I have read the qt doc about event and I believe that I need either method 1 or 2 to process mouse event.

1.Installing an event filter on qApp An event filter on qApp monitors all events sent to all objects in the application.

2.Reimplementing QApplication::notify(). Qt's event loop and sendEvent() call this function to dispatch events. By reimplementing it, you get to see events before anybody else.

They also seems powerful to me, but I don't understand their difference. Which one suits my requirement? Thank You.

Upvotes: 1

Views: 515

Answers (1)

Tim Meyer
Tim Meyer

Reputation: 12600

You can basically achieve the same thing with either solution except for the fact that QApplication::notify (or its override) will be called before any event filter that may be on your application.

As the first approach does not require subclassing QApplication, it usually is the preferred one.The only reason to override QApplication::notify in your case would be if you needed to override it due to other reasons anyway, e.g. because you need to do anything related to your own custom events.

But looking at your requirements I would personally go for the following solution:

  1. Install an event filter on qApp
  2. Create a timer with a 30 seconds interval
  3. Connect the timer to the lock screen method
  4. Have your event filter reset the timer every time a mouse press is detected.

Dependent on your application you might also want to look for KeyPress events and maybe MouseMove events as well.

Upvotes: 2

Related Questions