Results 1 to 16 of 16

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

Threaded 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.

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.