Datetime QT QML如何将毫秒转换为时间hh:mm:ss:zzz?
QT-QML中是否有一个简单的函数可以将毫秒转换为用户可读的时间,格式为hh:mm:ss:zzz?我找到了tis文档,但我真的不知道如何在我的案例中使用它。这是到目前为止我的代码,但它只显示秒和毫秒Datetime QT QML如何将毫秒转换为时间hh:mm:ss:zzz?,datetime,qml,Datetime,Qml,QT-QML中是否有一个简单的函数可以将毫秒转换为用户可读的时间,格式为hh:mm:ss:zzz?我找到了tis文档,但我真的不知道如何在我的案例中使用它。这是到目前为止我的代码,但它只显示秒和毫秒 import QtQuick 2.8 import QtQuick.Controls 2.1 Rectangle { id: mapItem anchors.fill: parent property int count: 0 Timer { id: timer
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"}
}
经过测试,我已按预期格式化计数器,请参见下文
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
,除非计时器不用于实时秒数
希望这有帮助 在上测试,我已按预期格式化计数器,请参见下文
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
,除非计时器不用于实时秒数
希望这有帮助 可能的重复可能的重复