8

How can I detect user inactivity in a Qt QMainWindow? My idea so far is to have a QTimer that increments a counter, which, if a certain value is passed, locks the application. Any mouse or key interaction should set the timer back to 0. However I need to know how to properly handle input events which reset; I can re-implement:

virtual void keyPressEvent(QKeyEvent *event)
virtual void keyReleaseEvent(QKeyEvent *event)
virtual void mouseDoubleClickEvent(QMouseEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void mouseReleaseEvent(QMouseEvent *event)

...but won't the event handlers of all the widgets in the QMainWindow prevent events occurring in those controls from reaching the QMainWindow's? Is there a better architecture for detecting user activity as it is?

Jake Petroules
  • 23,472
  • 35
  • 144
  • 225

3 Answers3

14

You could use a custom event filter to process all keyboard and mouse events received by your application before they are passed on to the child widgets.

class MyEventFilter : public QObject
{
  Q_OBJECT
protected:
  bool eventFilter(QObject *obj, QEvent *ev)
  {
    if(ev->type() == QEvent::KeyPress || 
       ev->type() == QEvent::MouseMove)
         // now reset your timer, for example
         resetMyTimer();

    return QObject::eventFilter(obj, ev);
  }
}

Then use something like

MyApplication app(argc, argv);
MyEventFilter filter;
app.installEventFilter(&filter);
app.exec();

This definitely works (I've tried it myself).

EDIT: And many thanks to ereOn for pointing out that my earlier solution was not very useful.

Greg S
  • 12,333
  • 2
  • 41
  • 48
  • Works great, thanks. I ended up emitting a signal with the obj parameter where you have "resetMyTimer", and had each window attach the signal to its reset timer slot, which checks to see if a widget on *that* window generated the event so each window locks independently of the others. – Jake Petroules Jul 28 '10 at 03:21
  • The event only seems to only be sent to eventFilter() if none of the child widgets handle said event. Is there a way to ensure all events go to this filter before being processed elsewhere? – KyleL Sep 24 '14 at 16:17
1

One of better approach will be to catch xidle signal rather then catching so many events from user. Here one need to capture QEvent:MouseMove event also

Vivek
  • 473
  • 1
  • 3
  • 10
0

The cleanest way is to override the eventFilter function of your main window widget and set it as event filter on your application object.

Inside the filter you can use dynamic_cast to check if the event is a QInputEvent. All events with user interaction are derived from QInputEvent and are recognized this way.

class MainWindow: public QWidget {
public:
    MainWindow() {
        QApplication::instance()->installEventFilter(this);
    }

protected:
    bool eventFilter(QObject* target, QEvent* event) override {
        if(dynamic_cast<QInputEvent*>(event)){
            // detected user interaction
        }

        return QWidget::eventFilter(target, event);
    }
};

You can replace the base class QWidget with any class derived from QWidget. (Including QMainWindow.) Note that the event function must pass the event to the base class, and return its return value.

If you have more then one window, you might also want to check, that the event target object is your window or one of its QWidget children.

class MainWindow: public QWidget {
public:
    MainWindow() {
        QApplication::instance()->installEventFilter(this);
    }

protected:
    bool eventFilter(QObject* target, QEvent* event) override {
        if(dynamic_cast<QInputEvent*>(event) && (
            target == this ||
            findChildren<QWidget*>().contains(target))
        ){
            // detected user interaction
        }

        return QWidget::eventFilter(target, event);
    }
};
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51