QML Timer详解

1.简介

计时器可以用来触发一次动作,也可以在给定的间隔内重复触发。

2.自动触发计时器

Window {
    id: window
    objectName: "window"
    visible: true
    width: 400
    height: 500
    title: qsTr("Hello World")

    property int time: 0

    Text{
        id:txt
        text:time
        anchors.centerIn: parent
        font.bold: true
        font.pointSize: 40
    }

    Timer{
        id:timer
        interval: 1000  //步长
        repeat: true    //自动重复
        running: true   //启动计时器
        onTriggered: {  //触发槽函数
            time += 1
        }

    }
}

3.手动触发计时器

Window {
    id: window
    objectName: "window"
    visible: true
    width: 400
    height: 500
    title: qsTr("Hello World")

    property int time: 0

    Text{
        id:txt
        text:time
        anchors.centerIn: parent
        font.bold: true
        font.pointSize: 40
    }

    Timer{
        id:timer
        interval: 1000  //步长
        repeat: true    //自动重复
        onTriggered: {  //触发槽函数
            time += 1
        }
    }

    Button{
        width: 50
        height: 50
        onClicked: {
            timer.running?timer.stop():timer.start()
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wzz953200463/article/details/129719798
QML