qt - Qml 计时器未在正确的时间间隔内触发

标签 qt timer qml qt5 qt-quick

我创建了一个计时器 QML 应用程序,并且我正在使用 Timer qml 组件。
间隔设置为 1000 毫秒(默认值)......但它似乎只有在应用程序专注于它时才能正常工作。
当我把它放在后台时,它似乎不是每次都触发,因此我在应用程序中遇到了一些错误。

我试图在文档中找到与此相关的任何内容,但我找不到
定时器代码非常简单:

Timer {
    id: timer
    repeat: true
    onTriggered: {msRemaining -= 1000; Core.secondsToText(type);}
}

任何人都对此有任何想法以及如何解决它?

版本:
Qt 5.2
QML 2.0
OS X 10.9

最佳答案

QML Timer 元素与动画计时器同步。由于动画定时器通常设置为 60fps,因此 Timer 的分辨率最多为 16ms。您还应该注意,在 Qt Quick 2 中,动画计时器与屏幕刷新同步(而在 Qt Quick 1 中,它被硬编码为 16 毫秒)。因此,当您的应用程序在后台运行时,我认为刷新已停止,因此与屏幕刷新同步的计时器将停止正常工作。

如果你想像你一样使用计时器显示耗时并不是一个好主意,因为它不精确。您可以使用 javascript Date() 函数,例如:

import QtQuick 2.0

Item  {
    id: root
    width: 200; height: 230

    property double startTime: 0
    property int secondsElapsed: 0

    function restartCounter()  {

            root.startTime = 0;

        }

    function timeChanged()  {
        if(root.startTime==0)
        {
            root.startTime = new Date().getTime(); //returns the number of milliseconds since the epoch (1970-01-01T00:00:00Z);
        }
        var currentTime = new Date().getTime();
        root.secondsElapsed = (currentTime-startTime)/1000;
    }

    Timer  {
        id: elapsedTimer
        interval: 1000;
        running: true;
        repeat: true;
        onTriggered: root.timeChanged()
    }

    Text {
        id: counterText
        text: root.secondsElapsed
    }
}

关于qt - Qml 计时器未在正确的时间间隔内触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752201/

相关文章:

c++ - C++/Qt 中的闭合轮廓

Python threading.Timer 对象在编译为 .exe 时不起作用

java - 用按钮停止计时器

c++ - qt 中对 QDeclarativePropertyMap 的 undefined reference

qt - 无法在 Qt 项目中使用共享库

c++ - Qt addApplicationFont 失败

c++ - 指定 QMenu 的左上角位置

python - python threading.Timer 如何确定耗时?

c++ - 触发属性更改的 QML 动画

qt - QML 窗口调整大小/移动闪烁