为什么在多线程中有时候QTimer超时事件不触发
Qt文档中关于QTimer有以下描述:
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.
在多线程程序中,你可以在**任何具有事件循环的线程**中使用QTimer.
如果在非GUI线程中启动了一个定时器,需要加上QThread::exec().
并且QTimer的启动和停止必须在同一线程。
所以:可以在启动定时器的线程中添加QThread::exec(),或者在线程中添加一个定时器启动信号,然后在GUI线程中处理,这样就将QTimer放到了GUI线程中