Display time is automatically updated every minute.

I have a QML application in which I am trying to create a box with simple clocks that display the current time - similar to the ones that exist on each operating system.

It is assumed that the time should be presented to the user as text in a format hh:mm, for example, 16:12.

I am currently trying to find a solution with the Timer component running during the life of the application and updating the text by calling:

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

every 60 seconds. Is there a better way to do this or to use the Timer component .

Snippet of the whole code:

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")
    }
}
+4
source share
1 answer

, , , ( SailfishOS);

;

    property alias updatesEnabled: timeText.updatesEnabled

:

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

/. , .

0

Source: https://habr.com/ru/post/1530449/


All Articles