title: QThread and QTimer
body: <p>I'm working on an application developed with Qt 4.6.</p>
  
  <p>I want to create a custom timer that counts in a separate thread. However, I want this timer to be able to send signals to the main thread.</p>
  
  <p>I subclassed QThread but it doesn't seem to work.</p>
  
  <p>Here is Timer.h:</p>
  
  <pre><code>#ifndef TIMER_H
  #define TIMER_H
  
  #include &lt;QtCore/QObject&gt;
  #include &lt;QtCore/QThread&gt;
  #include &lt;QtCore/QTimer&gt;
  
  class Timer : public QThread
  {
      Q_OBJECT
  public:
      explicit Timer(QObject *parent = 0);
      ~Timer();
  
      // true if the timer is active
      bool isCounting();
  
      // start the timer with a number of seconds
      void startCounting(int value = 300);
      void stopCounting();
  
      // the number of seconds to reach
      int maximum();
  
      // the current value of the timer
      int value();
  
      // elapsed time since the timer has started
      int elapsedTime();
  
  signals:
      // sent when the timer finishes to count
      void timeout();
      // an event is emited at each second when the timer is active
      void top(int remainingSeconds);
  
  protected:
      // launch the thread
      //virtual void run();
  
  private slots:
      // decrements the remaining time at each second and emits top()
      void timerEvent();
  
  private:
      QTimer* _timer;
      // remaining time
      int _left;
      // number of seconds at timer startup
      int _maximum;
  };
  
  #endif // TIMER_H
  </code></pre>
  
  <p>And Timer.cpp:</p>
  
  <pre><code>#include "Timer.h"
  
  Timer::Timer(QObject *parent) :
      QThread(parent)
  {
      _timer = new QTimer(this);
      _maximum = 0;
      _left = 0;
      connect(_timer, SIGNAL(timeout()), this, SLOT(timerEvent()));
  }
  
  Timer::~Timer()
  {
      delete _timer;
  }
  
  bool Timer::isCounting()
  {
      // test if timer still active
      return _timer-&gt;isActive();
  }
  
  void Timer::startCounting(int value)
  {
      qDebug() &lt;&lt; QString("Start timer for %1 secs").arg(QString::number(value));
      if(_left != 0 || _timer-&gt;isActive())
      {
           _timer-&gt;stop();
      }
  
      _maximum = value;
      _left = value;
  
      // emit the first top
      emit top(_left);
  
      // start the timer: 1000 msecs
      _timer-&gt;start(1000);
  
      // start the thread
      start();
  }
  
  void Timer::stopCounting()
  {
      qDebug() &lt;&lt; QString("Stopping timer at %1 secs =&gt; %2 secs remaining.").arg(QString::number(elapsedTime()), QString::number(_left));
      // stop timer
      _timer-&gt;stop();
      _left = 0;
      _maximum = 0;
      // kill thread
      terminate();
  }
  
  int Timer::maximum()
  {
      return _maximum;
  }
  
  int Timer::value()
  {
      return _left;
  }
  
  void Timer::timerEvent()
  {
      qDebug() &lt;&lt; "Timer event";
      if(--_left == 0)
      {
          // stop timer
          _timer-&gt;stop();
          // emit end of timer
          emit timeout();
          // stop thread
          terminate();
      }
      else
      {
          // emit a signal at each second
          emit top(_left);
      }
  }
  
  int Timer::elapsedTime()
  {
      return (_maximum - _left);
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I realized the object I tried to move to another thread was actually a singleton. It could lead to a problem (<a href="http://en.wikipedia.org/wiki/Singleton_pattern#Implementation" rel="nofollow">see here</a>).</p>