我有一個ListView,其代表是一個紅色按鈕。當按鈕的顏色發生變化時,我希望程序動態創建一個定時器(特定於該代表),該定時器在5秒後再次將顏色設置爲紅色。然後我想讓程序銷燬計時器。我該怎麼做?QML - 發生事件時動態創建定時器
這裏是我的實際代碼:
ListView {
id: myListView
model: myListModel
anchors.fill: parent
anchors.leftMargin: 20; anchors.rightMargin: 20
orientation: Qt.Vertical
clip: true
spacing: 8
delegate: Button {
id: myDelegate
property int myDelegateIndex: index + 1
width: 100; height: 50
text: "Push"
background: Rectangle {
id: myDelegateBackground
color: "red"
onColorChanged: {
myTimer.start();
}
}
Timer {
id: myTimer
interval: 5000
running: true
repeat: true
onTriggered: {
myDelegateBackground.color = "red";
}
}
}
}
謝謝大家了很多!
爲什麼要在使用它之後銷燬計時器?爲什麼不把「repeat」屬性設置爲false,然後它只會觸發一次。也將「running」設置爲false,以便在按下按鈕之前不會啓動。當代理被銷燬時,Timer組件將被銷燬,你不需要手動去做 –