【问题标题】:QT QML how to convert milliseconds to time hh:mm:ss:zzz?QT QML如何将毫秒转换为时间hh:mm:ss:zzz?
【发布时间】:2019-03-24 12:20:16
【问题描述】:

QT QML 中是否有一个简单的函数可以将毫秒转换为格式为 hh:mm:ss:zzz 的用户可读时间?我找到了 tis 文档https://doc.qt.io/qt-5/qml-qtqml-date.html,但我真的不明白如何在我的情况下使用它。到目前为止,这是我的代码,但它只显示秒和毫秒。

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

【问题讨论】:

标签: datetime qml


【解决方案1】:

在Felgo Web Editor上测试,我已经按照预期格式化了计数器,请看下面!;

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

请记住,如果您正在格式化日期,您可以将 toLocaleTimeString 替换为 toLocaleDateString 或任何最适合每个用例的内容。

更新!如果您正在寻找实时计数,您可能还想将count += 1 更改为+= 16 以及间隔,除非计时器不是用于实时秒数

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多