問題描述
我在玩 Qt,我想創建一個兩個命令之間的簡單暫停.但是它似乎不會讓我使用 Sleep(int mili);
,而且我找不到任何明顯的等待函數.
I'm playing around with Qt, and I want to create a simple pause between two commands. However it won't seem to let me use Sleep(int mili);
, and I can't find any obvious wait functions.
我基本上只是制作一個控制臺應用程序來測試一些類代碼,這些代碼稍后將包含在適當的 Qt GUI 中,所以現在我不擔心破壞整個事件驅動模型.
I am basically just making a console application to test some class code which will later be included in a proper Qt GUI, so for now I'm not bothered about breaking the whole event-driven model.
推薦答案
這個 上一個問題 提到使用 qSleep()
,它位于 QtTest
模塊中.為了避免 QtTest
模塊中的開銷鏈接,查看該函數的源代碼,您可以制作自己的副本并調用它.它使用定義來調用 Windows Sleep()
或 Linux nanosleep()
.
This previous question mentions using qSleep()
which is in the QtTest
module. To avoid the overhead linking in the QtTest
module, looking at the source for that function you could just make your own copy and call it. It uses defines to call either Windows Sleep()
or Linux nanosleep()
.
#ifdef Q_OS_WIN
#include <windows.h> // for Sleep
#endif
void QTest::qSleep(int ms)
{
QTEST_ASSERT(ms > 0);
#ifdef Q_OS_WIN
Sleep(uint(ms));
#else
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
#endif
}
這篇關于如何使用 Qt 創建暫停/等待功能?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!