Qml timer does not start at the correct interval

I created a QML application with a timer and I am using the qmer Timer component. The interval is set to 1000 milliseconds (default) ... but it only works correctly when the application is focused on it. When I put it in the background, it seems that it does not start every time, and because of this I got some errors in the application.

I tried to find something related to this in the documentation, but I could not. The timer code is very simple:

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

Does anyone know about this and how to fix it?

Version: Qt 5.2 QML 2.0 OS X 10.9

+7
qt timer qt5 qml qt-quick
source share
3 answers

The QML timer element synchronizes with the animation timer. Since the animation timer is usually set to 60 frames per second, the resolution of the timer will be 16 ms at best. It should also be noted that in Qt Quick 2, the animation timer synchronizes with the screen update (while in Qt Quick 1 it is hardcoded to 16 ms). Therefore, when your application runs in the background, I think the update is stopped, and therefore your timer, synchronized with the screen update, will stop working normally.

If you want to show the elapsed time with a timer, how you did it, this is not a good idea, because it is not accurate. You can use javascript Date () function, for example:

 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 } } 
+9
source share

I have a QML application with a Timer object running on Android:

  • With Qt 4.8, Timer works great when a QML application is in the background.

  • With Qt 5.4, the timer no longer works when the QML application is in the background. For example, a QML application can no longer receive the onTriggered () signal. When the QML application returns to the forefront, the timer starts working again. It seems that the Qt signals are blocked and the QML application is in the background.

So it looks like a regression in Qt. The best solution would be to wait until this regression is fixed.

+1
source share

This piece of code works for me, it should be inside ApplicationWindow{} or Item{} elements.

  Timer{ id: closeTimer interval: 10000 repeat: true running: true onTriggered: { drawer.close() } } 

Both repeat and run must be true.

0
source share

All Articles