qt 子线程中Qtimer的使用一般不生效,因为线程的维持一般都是满耗时任务状态来维持,没有exec 或者 轮不到 exec起作用。
timer 需要exec 也是标准事件循环,这也是timer为什么能再主线程的中生效的原因。
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.
注意上面的事项一般就没有问题了!
真不行还有一种办法:
再开一个线程放 timer 对象
timer = new QTimer();
timer->start(100);
timerThread = new QThread;
timer->moveToThread(timerThread);
connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar()), Qt::DirectConnection);
timerThread->start();