I'm trying to display some text based on what time it is, and I have a time property that stores the current time. I want the time on the text to be updated as time passes, but it never updates, and is stuck on the time when it first ran. And since the time never updates, I can't use the visible
condition to display my text. What can I do to fix this code to make sure the values update here?
import QtQuick 2.0
import QtQuick.Window 2.2
Window {
visible: true
height: 400
width: 400
property real time: new Date().getTime()
property real hour: new Date().getHours()
Rectangle {
id: rect
width: 400; height: 400
color: "white"
Text {
anchors.centerIn: parent
text: "Good morning, the time is: " time
// visible: hour > 4 && hour < 12
}
}
}
CodePudding user response:
Your binding won't work because you're binding to a javascript function that doesn't send a changed signal. You need to use a Timer for this:
Window {
visible: true
height: 400
width: 400
property real time: new Date().getTime()
property real hour: new Date().getHours()
Timer {
running: true
repeat: true
interval: 60000 // Update every 60 seconds maybe?
onTriggered: {
time = new Date().getTime()
hour = new Date().getHours()
}
}
Rectangle {
id: rect
width: 400; height: 400
color: "white"
Text {
anchors.centerIn: parent
text: "Good morning, the time is: " time
visible: hour > 4 && hour < 12
}
}
}