QObject::startTimer: Timers can only be used with threads started with QThread

末鹿安然 提交于 2019-12-01 06:56:10

问题


I am trying to start a Timer in a worker thread's event loop, but I get this error: QObject::startTimer: Timers can only be used with threads started with QThread

Whats wrong with this?

#include <QObject>
#include <QThread>
#include <QTimer>

class A : public QObject
{
    Q_OBJECT
public:
    A();

private:
    QThread m_workerThread;
    QTimer m_myTimer;

};

A::A()
{
    this->moveToThread(&m_workerThread);
    m_myTimer.moveToThread(&m_workerThread);
    m_workerThread.start();
    m_myTimer.start(1000);
}

回答1:


I Think i figured it out, i tried to start the timer from the GUI thread, after i moved it to the worker thread, this way it seems to work:

class A : public QObject
{
    Q_OBJECT
public:
    A();

private:
    QThread m_workerThread;
    QTimer m_myTimer;

public slots:
    void sl_startTimer();
};

A::A()
{
    this->moveToThread(&m_workerThread);
    m_myTimer.moveToThread(&m_workerThread);
    m_workerThread.start();
    QMetaObject::invokeMethod(this, "sl_startTimer", Qt::QueuedConnection);
}

void A::sl_startTimer()
{
    m_myTimer.start(1000);
}



回答2:


Initialize your timer anywhere, but start it right when the thread is started (attach it to QThread::started signal):

class A : public QObject
{
    Q_OBJECT
public:
    A();

private slots:
    void started();
    void timeout();

private:
    QThread m_workerThread;
    QTimer m_myTimer;
};

A::A()
{
    moveToThread(&m_workerThread);

    connect(&m_workerThread, SIGNAL(started()), this, SLOT(started()));
    connect(&m_myTimer, SIGNAL(timeout()), this, SLOT(timeout()));

    m_myTimer.setInterval(1000);
    m_myTimer.moveToThread(&m_workerThread);

    m_workerThread.start();
}

void A::started()
{
    timer.start();
}

void A::timeout()
{
    // timer handler
}



回答3:


This approach seems a little dangerous to me. By moving the QObject onto the QThread, you're making the thread responsible for the object's events (signals, slots, messages, etc). When the object is deleted, however, the thread will be deleted before the object itself, which can lead to some unexpected behaviours.

The recommended approach is to instantiate the thread and the object separately.



来源:https://stackoverflow.com/questions/22399868/qobjectstarttimer-timers-can-only-be-used-with-threads-started-with-qthread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!