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

dan*_*nca 7 qt timer qml qt-quick qt5

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

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

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

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

版本:Qt 5.2 QML 2.0 OS X 10.9

Nej*_*jat 9

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

如果您想使用计时器显示已用时间,则不是一个好主意,因为它不准确.您可以使用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
    }
}
Run Code Online (Sandbox Code Playgroud)