QML Timer - 如何提高准确性?

2022-01-19 00:00:00 qt qml c++ qtcore qelapsedtimer

我正在开发 QML 节拍器.我使用了一个间隔为 60000/Beats 每分钟的计时器.然而,它并不准确.我怎样才能提高准确性.我应该使用计时器,还是有更好的解决方案?

I'm developing a QML metronome. I used a timer with interval 60000/Beats per minute. However it isn't really accurate. How can I improve the accuracy. Should I use a Timer, or is there a better solution?

推荐答案

QTimer 的基本问题是它使用 Qt 事件循环进行计时.不幸的是,它天生就不够准确.通知的延迟以及事件循环中的所有内容都在阻碍.

The fundamental issue with QTimer that it uses the Qt event loop for the timing. Unfortunately, it cannot be accurate enough, inherently. The latency for notifications and all that within the event loop is getting in the way.

您需要考虑一个实际上并不高度依赖 Qt 事件循环的计时器,例如 Windows 上的 QueryPerformanceCounter().这就是我们进入 QElapsedTimer 领域的方式.

You would need to consider a timer that does not actually depend highly on the Qt event loop, like QueryPerformanceCounter() on Windows. That is how we get to the realm of QElapsedTimer.

因此,我会为此目的使用 QElapsedTimer.

Thereby, I would use QElapsedTimer for this purpose.

下面的帖子似乎为此目的实现了一个自定义类.您可以按原样使用它,然后根据需要对其进行调整以更好地满足您的需求.

The following post has a custom class implemented for this purpose as it seems. You may be able to take it as is, and then tweak it to suit your need even better if needed.

高分辨率计时器

相关文章