如何使用 CPP 在 qt 中制作 QTimer 作为空闲计时器
我是 Qt 编程新手,我想让计时器成为空闲计时器。 问题是仅仅设置timer->start(0)是否会使定时器成为空闲定时器? 我怎么知道它是一个空闲计时器。
I am new to Qt programming, I want to make the timer an idle timer.
The question is whether just setting the timer->start(0) will make the timer an idle timer?
How can I know it's an idle timer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想重复@Jeremy Friesner:
超时为 0 的应用程序永久占用(超过)一个核心并导致高货币消耗。
我们开始:间隔为 0 的
QTimer
:输出:
间隔 0 使计时器立即到期。这意味着超时事件被附加到事件队列中。
因此,我希望队列永久充满超时和绘制事件(用于更新
qLblI
),直到应用程序退出。我过去曾经使用过这样的空闲事件(在我开始使用Qt之前)。我打算将 UI 事件循环与来自不同来源的轮询“事件”结合起来(并且不知道更好的替代方案)。因此,轮询功能为自己提供了超时选项。因此,poll 函数的调用会自行暂停进程,直到有事件进入或达到超时为止。最复杂的事情是以某种方式实现负载平衡,因为我试图在 UI 和其他事件源的处理之间平均分配可用时间。 (这对于 UI 事件与其他事件源同时高频地接近的情况很重要。)
但是,我怀疑这种摆弄在 Qt 中是否仍然有必要。对于这样的并发性,有更好的选择,例如在单独的线程中运行阻塞函数。
I'd like to repeat the concerns of @Jeremy Friesner:
An application with timeout 0 occupies (more than) one core permanently and causes a high currency consumption.
Here we go: a
QTimer
with interval 0:Output:
The interval 0 makes the timer immediately due. This means that a timeout event is appended to the event queue.
Thus, I expect the queue permanently filled with timeout and paint events (for update of
qLblI
) until the application exits.I once used such an idle event in the past (before I started to use Qt). I intended to combine the UI event loop with polling "events" from a different source (and lacking any idea about a better alternative). Thereby, the polling function provided itself a time-out option. Thus, the call of the poll function suspended itself the process until either an event came in or the timeout was reached. The most intricate thing was to achieve somehow a load balancing as I tried to distribute the available time equally between the UI and the processing of the other event source. (This was important for the situation where UI events were approaching in high frequency concurrently with the other event source.)
However, I doubt that such a fiddling would be still necessary in Qt. For such a concurrency, there are better options e.g. to run a blocking function in a separate thread.