QML实现倒计时功能


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
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40770277/article/details/89310018
今日推荐