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)?

    Thanks
    Tim


  • Moderators

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

    MyWidget::MyWidget(QWidget* parent) : QWidget(parent)
    {
        qApp->installEventFilter(this);
    }
    
    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
                        }
                        break;
                }
         }
    
         return QWidget::eventFilter(watched,event);
    }
    


  • @raven-worx

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.