Qml 计时器未在正确的时间间隔内触发 [英] Qml timer not triggered in the right interval

查看:94
本文介绍了Qml 计时器未在正确的时间间隔内触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a timer QML app, and I'm using Timer qml component. The interval is set to 1000 milliseconds (the default one)... but it seems to be working properly only when the app is with focus on it. When I put it in background it seems that it's not triggered every time, and because of that I got some mistakes in the app.

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

I've tried to find anything related to that in the documentation, but I couldn't The timer code is really simple:

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

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

Anyone has any idea about that and how to fix it?

版本:Qt 5.2QML 2.0OS X 10.9

Versions: Qt 5.2 QML 2.0 OS X 10.9

推荐答案

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

The QML Timer element is synchronized with the animation timer. Since the animation timer is usually set to 60fps, the resolution of Timer will be at best 16ms. You should also note that in Qt Quick 2 the animation timer is synced to the screen refresh (while in Qt Quick 1 it is hard-coded to 16ms). So when your app runs in background i think the refreshing is stopped and consequently your timer which is synced to screen refresh will stop working properly.

如果您想像以前那样使用计时器显示经过的时间,则不是一个好主意,因为它不精确.您可以使用 javascript Date() 函数,例如:

If you want to show elapsed time using a timer as you did is not a good idea because it is not precise. You can use javascript Date() function like:

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
    }
}

这篇关于Qml 计时器未在正确的时间间隔内触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆