In multithreaded applications, you can use QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread::exec(). Qt uses the timer's thread affinity to determine which thread will emit the timeout() signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.
So I'd expect this code...
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QTimer timer;
timer.start(1000);
app.exec();
}
...to fail because the main thread, where I'm calling start
, is not a QThread
and Timers can only be used with threads started with QThread
QUESTION
Why doesn't this fail?