2014-03-31 81 views
7

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

我试图找到文档中涉及到的东西,但我不能 定时器代码是非常简单的:

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

任何人有关于如何解决它的任何想法?

版本: Qt的5.2 QML 2.0 OS X 10.9

回答

8

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

如果您想使用定时器显示流逝的时间,因为这样做不是一个好主意,因为它不准确。您可以使用像JavaScript日期()函数:

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

感谢您的回答。 如何在Qt Quick 2中显示已用时间的最佳方式? – danielfranca

+0

我编辑了显示答案的答案,以显示经过的时间。 – Nejat

+0

屏幕关闭5分钟后是否可能触发事件? –

1

我有一个Timer对象QML的应用程序,运行在Android上:

  • 有了Qt 4.8,定时器工作正常时,QML应用在后台。

  • 在Qt 5.4中,当QML应用程序在后台时Timer不再工作。例如,QML应用程序不能再接收onTriggered()信号。当QML应用程序再次返回到前台时,Timer再次开始工作。在QML应用程序处于后台时,似乎Qt信号被阻止。

所以这看起来像Qt中的回归。最好的解决办法是等到这个回归是固定的。

+0

这个回归似乎已经在Qt 5.5 alpha版本中得到修复。所以这个修复很可能在Qt 5.5官方发布中可用。 – jonathanzh