显示每分钟自动更新的时间

Mat*_*ski 5 qt qml

我有一个QML应用程序,其中我正在尝试一个显示当前时间的简单时钟 - 与每个操作系统中的类似.

时间应该作为文本格式呈现给用户hh:mm, 16:12.

目前我正在尝试在应用程序生命周期内运行Timer组件并通过调用更新文本的解决方案:

timeText.text = Qt.formatTime(new Date(),"hh:mm")

每60秒一次.有没有更好的方法来执行此操作或使用Timer组件是必要的.

整个代码的代码段:

Text {
    id: timeText
    x: 10
    y: 10
    text: Qt.formatTime(new Date(),"hh:mm")
}

Timer {
    id: timer
    interval: 60000
    repeat: true
    running: true

    onTriggered:
    {
        timeText.text =  Qt.formatTime(new Date(),"hh:mm")
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

我意识到这个问题已经有几年了,但没有给出解决方案,所以这就是我的做法(这适用于 SailfishOS);

添加属性;

    property alias updatesEnabled: timeText.updatesEnabled
Run Code Online (Sandbox Code Playgroud)

然后,重要的部分;

    Item {
    id: timeText
    color: timeText.color
    font.pixelSize: Theme.fontSizeHuge * 2.0
    text: { updatesEnabled: timeText.time  <--------add line here
        Qt.formatTime(new Date(), "hh:mm:ss")
    }
    anchors {
        horizontalCenter: parent.horizontalCenter
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的内容现在使时间/日期保持最新。我尝试在不同的行插入代码,但这是我可以让它工作的唯一方法。