我正在嘗試根據現在的時間顯示一些文本,并且我有一個存盤當前時間的時間屬性。我希望文本上的時間隨著時間的推移而更新,但它永遠不會更新,并且停留在它第一次運行的時間。而且由于時間永遠不會更新,我不能使用visible條件來顯示我的文本。我該怎么做才能修復此代碼以確保此處的值更新?
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
}
}
}
uj5u.com熱心網友回復:
您的系結將不起作用,因為您正在系結到一個不發送更改信號的 javascript 函式。為此,您需要使用 Timer:
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
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/452360.html
