Qt不像VC++的win32/MFC編程那樣,提供了現成的sleep函數可供調用。Qt把sleep函數封裝在QThread類中。子線程可以調用sleep函數。但是如果用戶想在主線程實現延時功能,該怎么辦呢?方法是自定義sleep延時函數。通過QDateTime來實現時間差。
#include <QDateTime> void MainWindow::sleep(int msec)//自定義Qt延時函數,單位毫秒 { QDateTime last = QDateTime::currentDateTime(); QDateTime now; while (1) { now = QDateTime::currentDateTime(); if (last.msecsTo(now) >= msec) { break; } } }
http://blog.csdn.net/libaineu2004/article/details/39161697