网上广泛流传一种延时:
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();
}
此延时亲测可用.