Initialize your timer anywhere, but start it right away when the thread is running (attach it to QThread :: started ):
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()
{
}
source
share