Important: Please read the Qt Code of Conduct -

How to monitor user activity on widget?

  • I have a user control widget with many input options for user.

    I want to set up a user activity timer and when it hits the end - hide this widget(later user can open it by clicking the screen).

    What I did for now is that I created new timer and placed timer->start() at each button press event, but this does not work everywhere.

    I tried to use *MousePressEvent * for widget, but this does not works for buttons.

    Is there anything that could restart my timer anytime user interacts with the widget (presses buttons or moves slider, switches between tabs)?


  • Moderators

    if you just care about any user input you can do this simple trick:

    MyWidget::MyWidget(QWidget* parent) : QWidget(parent)
    bool MyWidget::eventFilter(QObject* watched, QEvent* event)
         if( watched->isWidgetType() )
                QWidget* w = static_cast<QWidget*>(watched);
                switch( event->type() )
                        case QEvent::MouseButtonPress:
                        case QEvent::MouseButtonRelease:
                        case QEvent::KeyPress:
                        case QEvent::KeyRelease:
                        // remove the following if not needed
                        case QEvent::TouchBegin:
                        case QEvent::TouchUpdate:
                        case QEvent::TouchEnd:
                               if( this->isAncestorOf(w) )
                                   // restart timer here
         return QWidget::eventFilter(watched,event);

  • @raven-worx

    Thanks! Added default case to remove build warnings and it works fine!

Log in to reply