QML中计时器的使用,实现一个简单的倒计时程序

来源:互联网 发布:fifaonline3m数据错误 编辑:程序博客网 时间:2024/05/24 15:43

还是要说明,我是看QMLBook和Qt Quick核心编程这两本书边学边写的。所以以下代码尽管有所改动,但是仍旧应该是有版权的,来自Qt Quick核心编程这本书。


import QtQuick 2.3import QtQuick.Window 2.2import QtQuick.Controls 1.4Window {    visible: true    Rectangle{        id:gray        width: 100        height: 100        color: "gray"        anchors.centerIn: parent        QtObject{            id:attr            property int counter;            Component.onCompleted: {                counter=10            }        }    }    Text {        id: countshow        anchors.centerIn: parent        color: "blue"        font.pixelSize: 40    }    Timer{        id:countdown        interval: 1000        repeat: true        triggeredOnStart: true//这一设置保证了立即触发,如果没有,你会发现有延迟        onTriggered: {            countshow.text=attr.counter            attr.counter-=1;            if(attr.counter<0)            {                countdown.stop()                countshow.text="开始啦"            }        }    }    Button{        id:startBtn        text: "开始"        anchors.top: gray.bottom        anchors.horizontalCenter: gray.horizontalCenter        onClicked:  {            attr.counter=10            countdown.start()        }    }}

效果图;

貌似没有什么复杂的地方。


0 0