可以使用Timer组合Date对象来定时更新标签的文本。
Label {
id: timeLabel
text: "Time: "
font.pixelSize: 25//字体大小
anchors.bottom: parent.bottom//布局
Component.onCompleted: {
updateDateTime() // 在控件完成初始化后立即更新时间
}
function updateDateTime() {
var currentTime = new Date()
timeLabel.text = currentTime.toLocaleString(Qt.locale("de_DE"),"yyyy-MM-dd hh:mm:ss")
}
Timer {
interval: 1000 // 每秒更新一次时间
running: true // 定时器运行
repeat: true // 重复执行
onTriggered: {
timeLabel.updateDateTime()//每触发一次,更新一次label上时间显示
}
}
}
在上述代码中,创建了一个Timer对象来每秒触发一次定时器,并在定时器触发时调用updateDateTime()函数来更新日期和时间。
在Label的text属性中,我们将当前日期和时间的字符串设置为标签的文本。
通过定时器的interval属性,可以调整更新频率,此处设置为每秒钟更新一次。