QThread和QTimer

Max*_*ter 3 c++ qt qthread qtimer

我正在研究用Qt 4.6开发的应用程序.

我想创建一个在单独的线程中计数的自定义计时器.但是,我希望这个计时器能够向主线程发送信号.

我将QThread子类化,但它似乎不起作用.

这是Timer.h:

#ifndef TIMER_H
#define TIMER_H

#include <QtCore/QObject>
#include <QtCore/QThread>
#include <QtCore/QTimer>

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
Run Code Online (Sandbox Code Playgroud)

和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);
}
Run Code Online (Sandbox Code Playgroud)

编辑

我意识到我试图移动到另一个线程的对象实际上是一个单例.它可能导致问题(见这里).

dte*_*ech 12

在这种特殊情况下,您不需要继承QThread.一般来说,除非你确定它是你需要的,否则不要继承QThread.

以下是如何在线程中设置worker和timer并启动它的快速示例:

工人阶级:

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = 0) : QObject(parent) {}

signals:
    void doSomething();

public slots:
    void trigger() {
        emit doSomething();
    }
};
Run Code Online (Sandbox Code Playgroud)

main.cpp中

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MainThreadObject o;

    QThread *thread = new QThread;
    Worker w;
    QTimer timer;
    timer.setInterval(1000);

    timer.moveToThread(thread);
    w.moveToThread(thread);

    QObject::connect(thread, SIGNAL(started()), &timer, SLOT(start()));
    QObject::connect(&w, SIGNAL(doSomething()), &o, SLOT(doSomething()));
    QObject::connect(&timer, SIGNAL(timeout()), &w, SLOT(trigger()));

    thread->start();

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

所以,我们有MainThreadObject代表一个QObject派生的生活在主线程中.我们创建了计时器和Worker对象,它只用于包装信号和插槽,以避免需要子类化QThread.定时器被设置并且它和工作者被移动到新线程,线程started()信号连接到定时器start()槽,工作者doSomething()信号连接到主线程对象doSomething()槽,最后定时器timeout()信号连接到工作trigger()槽.然后启动线程,在事件循环中启动整个链.

结果,MainThreadObject::doSomething()每秒调用一次,从辅助线程发出信号.

  • 请注意,您可以在主线程中执行此操作,计时器本身不会繁重工作,因此只要您不阻止主线程,它就不需要在新线程中.我刚刚做了这个例子,向你展示如何将计时器放在一个新的线程中,以备你需要的,因为这就是你要求的. (2认同)