網上廣泛流傳一種延時:
QTime dieTime = QTime::currentTime().addMSecs(mSec);
while (QTime::currentTime() < dieTime) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 50); //可設定最小間隔
}
QCoreApplication::processEvents是個靜态函數, 存在延時被修改的可能性.
假設點選一個按鍵, 用上面這個函數延時50ms, 但同時同一線程的定時器延時了2000ms,那麼50ms就會被更新成2000ms!
挖了個大坑- -!
void delay(int mSec)
{
QEventLoop loop;
QTimer::singleShot(mSec, &loop, SLOT(quit()));
loop.exec();
}
此延時親測可用.