如何使用 Qt 给出循环执行的延迟

发布于 2024-09-25 17:23:47 字数 58 浏览 2 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瘫痪情歌 2024-10-02 17:23:47

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

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

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

这是创建您自己的 *sleep 方法的代码。

#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);}
};

你可以这样调用它:

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

这会给你相应的 10 微秒、10 毫秒或 10 秒的延迟。如果底层操作系统计时器支持该分辨率。

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

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);

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);

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

审判长 2024-10-02 17:23:47

作为 @Live 的答案的更新,对于 Qt ≥ 5.2,不再需要子类 QThread,现在睡眠功能是公开的:

静态公共成员

  • QThread * currentThread()
  • Qt::HANDLE currentThreadId()
  • int IdealThreadCount()
  • void msleep(unsigned long msecs)
  • 无效睡眠(无符号长秒)
  • void usleep(unsigned long usecs)
  • void YieldCurrentThread()

cf http://qt-project.org/doc/qt-5/qthread.html #静态公共成员

As an update of @Live's answer, for Qt ≥ 5.2 there is no more need to subclass QThread, as now the sleep functions are public:

Static Public Members

  • QThread * currentThread()
  • Qt::HANDLE currentThreadId()
  • int idealThreadCount()
  • void msleep(unsigned long msecs)
  • void sleep(unsigned long secs)
  • void usleep(unsigned long usecs)
  • void yieldCurrentThread()

cf http://qt-project.org/doc/qt-5/qthread.html#static-public-members

淑女气质 2024-10-02 17:23:47

C++11 有一些便携式定时器的东西。查看 sleep_for。

C++11 has some portable timer stuff. Check out sleep_for.

滥情稳全场 2024-10-02 17:23:47

所以这个问题已经有近 10 年的历史了,但它在我的一次搜索中突然出现,我认为在 Qt 中编程时有更好的解决方案:信号与信号。槽、定时器和有限状态机。所需的延迟可以在不以中断其他功能的方式休眠应用程序的情况下实现,并且无需并发编程且无需旋转处理器 - Qt 应用程序将在没有事件要处理时休眠。

解决这个问题的一个技巧是让一系列计时器的 timeout() 信号连接到事件的槽,然后启动第二个计时器。这很好,因为它很简单。这不太好,因为如果存在逻辑分支,那么很快就会变得难以排除故障和维护,而逻辑分支通常出现在任何玩具示例之外。

QTimer

更好、更灵活的选择是 Qt 中的状态机基础设施。在那里,您可以为具有多个状态和分支的任意事件序列配置框架。随着时间的推移,FSM 更容易定义、扩展和维护。

Qt 状态机

So this question is nearly 10 years old, but it popped up on one of my searches, and I think that there are better solutions when programming in Qt: Signals & slots, timers, and finite state machines. The delays that are required can be implemented without sleeping the application in a way that interrupts other functions, and without concurrent programming and without spinning the processor - the Qt application will sleep when there are no events to process.

A hack for this is to have a sequence of timers with their timeout() signal connected to the slot for the event, which then kicks off the second timer. This is nice because it is simple. It's not so nice because it quickly becomes difficult to troubleshoot and maintain if there are logical branches, which there generally will be outside of any toy example.

QTimer

A better, more flexible option is the State Machine infrastructure within Qt. There you can configure an framework for an arbitrary sequence of events with multiple states and branches. An FSM is much easier to define, expand and maintain over time.

Qt State Machine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文