The line
QObject::connect(&timer,
SIGNAL(timeout
),
this,
SLOT(onTimer
()));
QObject::connect(&timer, SIGNAL(timeout), this, SLOT(onTimer()));
To copy to clipboard, switch view to plain text mode
connects the timer's signal to a slot of the QThread object itself. The QThread object lives in the main thread, not in the thread it manages. Therefore the connection behaves as a queued connection, and the slot will only be executed if you start the event loop in the main thread.
You could instead move the slot to a new object, e.g. MyObject, that lives in the worker thread, e.g.:
Q_OBJECT
/* ... */
public slots:
void onTimer();
}
/* ... */
virtual void MyThread::run() {
MyObject myObject;
QObject::connect(&timer,
SIGNAL(timeout
),
&myObject,
SLOT(onTimer
()));
timer.start(1000);
}
class MyObject : public QObject {
Q_OBJECT
/* ... */
public slots:
void onTimer();
}
/* ... */
virtual void MyThread::run() {
QTimer timer;
MyObject myObject;
QObject::connect(&timer, SIGNAL(timeout), &myObject, SLOT(onTimer()));
timer.start(1000);
QThread::exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks