Results 1 to 6 of 6

Thread: Detecting user inaction

  1. #1
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Detecting user inaction

    In Qt4, does anybody have a good idea of how determine if there is no user interaction with a program?

    I'd like to have a have a signal emited if the user has been inactive for x minutes. I was thinking I could have a single shot qtimer that is reset after any user event, by a installEventFilter function for the qmainwindow. Would this work? Is it the best solution. I wanted to get some advice before I ran off and spent a bunch of time playing. Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Detecting user inaction

    You might try to install an event filter for QApplication to monitor all events.

  3. #3
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Detecting user inaction

    Thanks, so I went with my original idea with jacek's suggestion of using the qapp rather than the main window, but the solution should be generic. Here is an example for anyone that might want to use a similar code.

    Header
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3.  
    4. class activityFilter : public QObject
    5. {
    6. Q_OBJECT
    7. Q_PROPERTY(int interval READ interval WRITE setInterval)
    8. public:
    9. activityFilter(QObject *parent = 0);
    10. int interval () const { return _interval; };
    11. void setInterval ( int msec ) { _interval = msec; };
    12. signals:
    13. void userInactive();
    14. public slots:
    15. void timeout();
    16. protected:
    17. bool eventFilter(QObject *obj, QEvent *event);
    18. private:
    19. QTimer *timer;
    20. int _interval;
    21. };
    To copy to clipboard, switch view to plain text mode 

    Implementation
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include "af.h"
    4.  
    5. activityFilter::activityFilter(QObject *parent) : QObject(parent), _interval(3000)
    6. {
    7. timer = new QTimer(this);
    8. timer->start(_interval);
    9. connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
    10. }
    11.  
    12. bool activityFilter::eventFilter(QObject *obj, QEvent *event)
    13. {
    14. if (event->type() != QEvent::Timer) // this is needed or else the timer is reset by its own timeout signal event before the signal is sent
    15. timer->start(_interval);
    16. return QObject::eventFilter(obj, event); // just a wrapper, we don't want to override any events
    17. }
    18.  
    19. void activityFilter::timeout()
    20. {
    21. qDebug() << "user timeout";
    22. emit userInactive();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Example
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include "ui_main.h"
    4. #include "af.h"
    5.  
    6. int main(int argc, char * argv[])
    7. {
    8. QApplication app(argc, argv);
    9. app.setQuitOnLastWindowClosed(true);
    10.  
    11. activityFilter *ef;
    12. ef = new activityFilter(&app);
    13. app.installEventFilter(ef);
    14.  
    15. QDialog win;
    16. Ui_dialog ui;
    17. ui.setupUi(&win);
    18.  
    19. QObject::connect(ef, SIGNAL( userInactive() ),
    20. &win, SLOT( close() ));
    21.  
    22. win.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    I created a quick dialog in designer saved as main.ui to make this work.

  4. The following 4 users say thank you to jrideout for this useful post:

    ePharaoh (27th March 2006), nupul (3rd April 2006), vasanth (24th September 2007), winifredrayen (9th December 2010)

  5. #4
    Join Date
    Jul 2007
    Location
    Bangalore, India
    Posts
    13
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Detecting user inaction

    hi man

    cool solution

    thanks a lot to u


    bye

  6. #5
    Join Date
    Dec 2010
    Location
    Chennai, India
    Posts
    6
    Thanks
    1
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows Symbian S60

    Default Re: Detecting user inaction

    @jrideout cool , thanks for the solution. I had a doubt while was i looking through ur code. If the filter object is "ef" and the monitored object is our "app" then the event monitoring is done across our app only right ? if we had to deal with idle time of the entire mobile then we need to monitor the parent process or say the main standby process isnt it ? can you brief on that ?
    Don't let ur hormones hijack your intelligence !
    Joe Winifred

  7. #6
    Join Date
    Dec 2010
    Location
    Chennai, India
    Posts
    6
    Thanks
    1
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows Symbian S60

    Default Re: Detecting user inaction

    cool , thanks for the solution. I had a doubt while was i looking through ur code. If the filter object is "ef" and the monitored object is our "app" then the event monitoring is done across our app only right ? if we had to deal with idle time of the entire mobile then we need to monitor the parent process or say the main standby process isnt it ? can you brief on that
    Don't let ur hormones hijack your intelligence !
    Joe Winifred

Similar Threads

  1. Logging Qt User Actions
    By Frank J. Lhota in forum Qt Programming
    Replies: 14
    Last Post: 30th May 2014, 21:36
  2. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2006, 23:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.