如何使用Qt延迟执行循环 [英] How to give a delay in loop execution using Qt

查看:850
本文介绍了如何使用Qt延迟执行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我希望执行循环时,每次控件转移到循环时,每次执行都必须延迟特定的时间.我该怎么办?

In my application I want that when a loop is being executed, each time the control transfers to the loop, each execution must be delayed by a particular time. How can I do this?

推荐答案

EDIT(删除了错误的解决方案). 编辑(添加此其他选项):

EDIT (removed wrong solution). EDIT (to add this other option):

另一种使用它的方法是QThread的子类,因为它具有受保护的* sleep方法.

Another way to use it would be subclass QThread since it has protected *sleep methods.

QThread::usleep(unsigned long microseconds);
QThread::msleep(unsigned long milliseconds);
QThread::sleep(unsigned long second);

下面是创建您自己的* sleep方法的代码.

Here's the code to create your own *sleep method.

#include <QThread>    

class Sleeper : public QThread
{
public:
    static void usleep(unsigned long usecs){QThread::usleep(usecs);}
    static void msleep(unsigned long msecs){QThread::msleep(msecs);}
    static void sleep(unsigned long secs){QThread::sleep(secs);}
};

您可以通过以下方式调用它:

and you call it by doing this:

Sleeper::usleep(10);
Sleeper::msleep(10);
Sleeper::sleep(10);

这将相应地为您提供10微秒,10毫秒或10秒的延迟.如果底层操作系统计时器支持该分辨率.

This would give you a delay of 10 microseconds, 10 milliseconds or 10 seconds, accordingly. If the underlying operating system timers support the resolution.

这篇关于如何使用Qt延迟执行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆