qt 子線程中Qtimer的使用一般不生效,因?yàn)榫€程的維持一般都是滿耗時(shí)任務(wù)狀態(tài)來(lái)維持,沒有exec 或者 輪不到 exec起作用。
timer 需要exec 也是標(biāo)準(zhǔn)事件循環(huán),這也是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.
注意上面的事項(xiàng)一般就沒有問(wèn)題了!
真不行還有一種辦法:
再開一個(gè)線程放 timer 對(duì)象
timer = new QTimer();
timer->start(100);
timerThread = new QThread;
timer->moveToThread(timerThread);
connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar()), Qt::DirectConnection);
timerThread->start();
--------------------
原文地址:https://soft.pbottle.com/a-13877.html