QThread и QTimer

Я работаю над приложением, разработанным с помощью Qt 4.6.

Я хочу создать собственный таймер, который рассчитывает в отдельном потоке. Тем не менее, я хочу, чтобы этот таймер мог отправлять сигналы в основной поток.

Я подкласс QThread, но это неКажется, не работает.

Вот Timer.h:

#ifndef TIMER_H
#define TIMER_H

#include 
#include 
#include 

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

И Timer.cpp:

#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->isActive();
}

void Timer::startCounting(int value)
{
    qDebug() < QString("Start timer for %1 secs").arg(QString::number(value));
    if(_left != 0 || _timer->isActive())
    {
         _timer->stop();
    }

    _maximum = value;
    _left = value;

    // emit the first top
    emit top(_left);

    // start the timer: 1000 msecs
    _timer->start(1000);

    // start the thread
    start();
}

void Timer::stopCounting()
{
    qDebug() < QString("Stopping timer at %1 secs => %2 secs remaining.").arg(QString::number(elapsedTime()), QString::number(_left));
    // stop timer
    _timer->stop();
    _left = 0;
    _maximum = 0;
    // kill thread
    terminate();
}

int Timer::maximum()
{
    return _maximum;
}

int Timer::value()
{
    return _left;
}

void Timer::timerEvent()
{
    qDebug() < "Timer event";
    if(--_left == 0)
    {
        // stop timer
        _timer->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);
}

Ответы на вопрос(4)

Ваш ответ на вопрос