QML定时器

时间:2023-08-10 23:15:40
QML定时器

QML中的定时器能够周期性的触发一个事件,其使用非常简单、方便。这里给出一个示例:

 import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2 Rectangle{
id: root
width: 512
height: 512
color: "gray" QtObject {
id: attrs
property int count;
Component.onCompleted: {
attrs.count = 10;
}
} Text {
id: countShow
color: "blue"
font.pixelSize: 30
anchors.centerIn: parent
} Timer {
id: countDown
interval: 1000
repeat: true
triggeredOnStart: true
onTriggered: {
countShow.text = attrs.count;
attrs.count -= 1;
if (attrs.count < 0){
countDown.stop();
countShow.text = "Clap Now!";
startBtn.text = "Start"
}
}
} Button {
id: startBtn
text: "Start"
onClicked: {
attrs.count = 10;
countDown.start();
startBtn.text = "Restart"
} anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
}
}