2

Is there a easy function in QT QML to convert milliseconds to a user readable time in the format hh:mm:ss:zzz ? I found tis documentation https://doc.qt.io/qt-5/qml-qtqml-date.html but i don't really understand how to use it in my case . Here is my code so far but it only displays seconds and millisenconds.

import QtQuick 2.8
import QtQuick.Controls 2.1


Rectangle {
id: mapItem
anchors.fill: parent

property int count: 0


    Timer {
      id: timer
      interval: 100
      running: true
      repeat: true
      onTriggered: count += 1
    }

    Text {

    text: (count / 10).toFixed(3)
    font.pixelSize: 20
    font.bold: true
    font.family: "Eurostile"}
 }
Markus
  • 319
  • 5
  • 18
  • Possible duplicate of [milliseconds to time in javascript](https://stackoverflow.com/questions/9763441/milliseconds-to-time-in-javascript) – folibis Mar 24 '19 at 15:00

1 Answers1

1

Tested on Felgo Web Editor, I have formatted the counter as intended, please see below!;

import Felgo 3.0
import QtQuick 2.5

App {
    id: app

    Rectangle {
        id: mapItem            
        property int count: 0
        Timer {
            id: timer
            interval: 10 // i set to 10 for testing for faster results, but worked on 100 also
            running: true
            repeat: true
            onTriggered: time.text = new Date(mapItem.count += 1).toLocaleTimeString(Qt.locale(), "hh " + "mm " + "ss " + "zzz") // you can change the formatting as you please
            }

        Text {
            id: time
            font.bold: true
            font.family: "Eurostile"
        }
    } 
}

Remember if you were formatting dates, you would replace the toLocaleTimeString with toLocaleDateString or whatever is best for each use-case.

Update! if you are looking for counting in realtime, you probably want to change the count += 1 to += 16 also with the interval, unless the timer is not intended for realtime seconds

Hope this helps!

Ldweller
  • 327
  • 2
  • 16