Results 1 to 16 of 16

Thread: Timer to find out if app id idle not working as expected

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Timer to find out if app id idle not working as expected

    I need to find out when my app is idle for say 20 seconds, and then do something. The logic I thought of is this: run a timer constantly with timeout set to 20 seconds, on any user action, cancel the timer and start it again. Then I found this code here do the same thing. This is the code:

    Qt Code:
    1. #include "IdleTimer.hpp"
    2.  
    3. // Static initialisers
    4. IdleTimer *IdleTimer::m_Instance = 0;
    5. QTimer *IdleTimer::m_timer = new QTimer();
    6. int IdleTimer::m_timeout = 0;
    7. QMutex IdleTimer::m_timeoutMutex;
    8.  
    9. /*!
    10. * Class constructor
    11. */
    12. IdleTimer::IdleTimer(QObject *parent, int seconds) :
    13. QObject(parent)
    14. {
    15. m_timeoutMutex.lock();
    16.  
    17. if (seconds)
    18. m_timeout = seconds;
    19.  
    20. Q_ASSERT(parent);
    21. Q_ASSERT_X(m_timeout, "IdleTimer Constructor", "The timeout must be specified in the first call to instance.");
    22.  
    23. parent->installEventFilter(this);
    24. m_timer->singleShot(m_timeout*1000, this, SLOT(idleTimeout()));
    25.  
    26. m_timeoutMutex.unlock();
    27. }
    28.  
    29. /*!
    30. * Either reset the timeout to a different value to restart the timer
    31. */
    32. void IdleTimer::start(int seconds/*=0*/) {
    33. m_timeoutMutex.lock();
    34.  
    35. if(seconds)
    36. m_timeout = seconds;
    37.  
    38. Q_ASSERT_X(m_timeout, "IdleTimer reset", "A timeout must be specified either in this call or in the first call to instance.");
    39.  
    40. m_timer->start(m_timeout*1000);
    41.  
    42. m_timeoutMutex.unlock();
    43. }
    44.  
    45. void IdleTimer::stop() {
    46. m_timeoutMutex.lock();
    47. m_timer->stop();
    48. m_timeoutMutex.unlock();
    49. }
    50.  
    51. /*!
    52. * The vent filter
    53. */
    54. bool IdleTimer::eventFilter(QObject *obj, QEvent *ev)
    55. {
    56. qDebug() << "Ah, caught!" << endl;
    57. if(ev->type() == QEvent::KeyPress ||
    58. ev->type() == QEvent::MouseMove)
    59. // now reset your timer, for example
    60. IdleTimer::m_timer->start();
    61.  
    62. // Must return to allow further processing
    63. return QObject::eventFilter(obj, ev);
    64. }
    65.  
    66. // Must return to allow further processing
    67. return QObject::eventFilter(obj, ev);
    68. }
    69.  
    70. /*slot*/
    71. void IdleTimer::idleTimeout() {
    72. qDebug("Application has been idle, emitting idle signal ...");
    73. emit idle();
    74. }
    To copy to clipboard, switch view to plain text mode 

    Header:

    Qt Code:
    1. #ifndef IDLETIMER_H
    2. #define IDLETIMER_H
    3.  
    4. #include <qobject>
    5. #include <qmutex>
    6. #include <qevent>
    7. #include <qtimer>
    8.  
    9. class IdleTimer : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. // Singleton class stuff
    14. static IdleTimer* instance(QObject *parent=0, int seconds=0) {
    15. static QMutex mutex;
    16. if (!m_Instance) {
    17. mutex.lock();
    18. m_Instance = new IdleTimer(parent, seconds);
    19. mutex.unlock();
    20. }
    21. return m_Instance;
    22. }
    23.  
    24. static void drop() {
    25. qDebug("IdleTimer dropped ...");
    26. static QMutex mutex;
    27. mutex.lock();
    28.  
    29. m_timer->stop();
    30. if(m_timer) delete m_timer;
    31.  
    32. if (m_Instance)
    33. delete m_Instance;
    34. m_Instance = 0;
    35. mutex.unlock();
    36. }
    37.  
    38. void start(int seconds = 0);
    39. void stop();
    40.  
    41. private:
    42. explicit IdleTimer(QObject *parent, int seconds);
    43. explicit IdleTimer() {}
    44. ~IdleTimer() {}
    45.  
    46. IdleTimer(const IdleTimer &); // hide copy constructor
    47. IdleTimer& operator=(const IdleTimer &); // hide assign op
    48.  
    49. static IdleTimer *m_Instance;
    50. static QTimer *m_timer;
    51.  
    52. static QMutex m_timeoutMutex;
    53. static int m_timeout;
    54.  
    55. signals:
    56. void idle();
    57.  
    58. private slots:
    59. void idleTimeout();
    60.  
    61. protected:
    62. bool eventFilter(QObject *obj, QEvent *ev);
    63. };
    64.  
    65. #endif // IDLETIMER_H
    To copy to clipboard, switch view to plain text mode 

    This is how I am using this (MyApplication extends QApplication)

    Qt Code:
    1. MyApplication:: MyApplication()
    2. {
    3. IdleTimer* timer = IdleTimer::instance(this,20);
    4. }
    To copy to clipboard, switch view to plain text mode 

    The problem is on app launch, whatever happens, whether I am active or idle, the Application has been idle, emitting idle signal ... gets printed after the first 20 seconds. Moving the mouse, clicking somewhere does not cancel the timer and restart it. But the qDebug within event-filter works correctly on moving/clicking, which means the event filter is working fine, code is coming upto that point, but somehow I am not able to restart the timer as expected.

    How do I fix this?
    Last edited by Cupidvogel; 29th December 2015 at 16:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Timer to find out if app id idle not working as expected

    You are not passing a parent to getInstance() so you are not installing an event filter.

    This all looks really complicated, why all the mutexes?

    Just create the IdleTimer object in main() and install it as an event filter on the Qt application object.

    Btw, there is also http://inqlude.org/libraries/kidletime.html if you want a tested solution.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Cupidvogel (29th December 2015)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timer to find out if app id idle not working as expected

    The IdleTimer constructor requires a non-NULL QObject parent, on which it installs the event filter to watch for mouse move and key press events. You don't provide one.

    MyApplication:: MyApplication()
    {
    IdleTimer* timer = IdleTimer::getInstance();
    }
    And this isn't your real code. IdleTimer has no getInstance() method. Even if you meant to write instance() instead, this code would crash and burn because the IdleTimer constructor requires a non-NULL parent pointer in order for this code to execute:

    parent->installEventFilter(this);
    I see I type more slowly than you and anda_skoa...

    This IdleTimer class is a little messed up anyway - as a singleton, you can have only one of them. If you call instance() a second time, with a different parent, nothing actually happens; because the singleton already exists, it doesn't create a new instance, which means an event filter is never installed on the new parent. The only way it really works is if you install it on the application object where it can filter all events for everything in the app.
    Last edited by d_stranz; 29th December 2015 at 16:27.

  5. The following user says thank you to d_stranz for this useful post:

    Cupidvogel (29th December 2015)

  6. #4
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Timer to find out if app id idle not working as expected

    Yep. That I posted by mistake (I was experimenting with different constructors and copy pasted disjoint versions of the files). I mentioned it above as well..

  7. #5
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Timer to find out if app id idle not working as expected

    I tried this in main.cpp of my app:

    Qt Code:
    1. Application app(argc, argv);
    2.  
    3. IdleTimer *timer = IdleTimer::instance(&app,5);
    4. app.installEventFilter(timer);
    To copy to clipboard, switch view to plain text mode 

    Still same result. The debug message always comes 20 seconds after the app launches, no mater what I do, like clicking or moving the mouse.

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Timer to find out if app id idle not working as expected

    Does the eventFilter method get called?

    Cheers,
    _

  9. #7
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Timer to find out if app id idle not working as expected

    Yes, that's the problem, it does! If I put a simple qDebug in the event filter if conditional, it prints out all the time when I move my mouse or click anything! I cannot understand how it just fails to clear the timer!

  10. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Timer to find out if app id idle not working as expected

    Weird.
    Have you tried stop/start?

    Cheers,
    _

Similar Threads

  1. MVC - canFetchMore() is not working as expected
    By prasad_N in forum Qt Programming
    Replies: 6
    Last Post: 13th October 2015, 11:41
  2. QDir not working as expected
    By BettaUseYoNikes in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2011, 16:55
  3. Replies: 2
    Last Post: 22nd December 2010, 10:37
  4. keyPressevent not working with timer.
    By T1001 in forum Newbie
    Replies: 5
    Last Post: 9th December 2010, 04:55
  5. QTableView not working as expected.
    By junxuan in forum Qt Programming
    Replies: 7
    Last Post: 30th July 2009, 08:17

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
  •  
Qt is a trademark of The Qt Company.