QML定时器

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

 1 import QtQuick 2.4

 2 import QtQuick.Controls 1.3

 3 import QtQuick.Window 2.2

 4 import QtQuick.Dialogs 1.2

 5 

 6 Rectangle{

 7     id: root

 8     width:  512

 9     height: 512

10     color: "gray"

11 

12     QtObject {

13         id: attrs

14         property int count;

15         Component.onCompleted: {

16             attrs.count = 10;

17         }

18     }

19 

20     Text {

21         id: countShow

22         color: "blue"

23         font.pixelSize: 30

24         anchors.centerIn: parent

25     }

26 

27     Timer {

28         id: countDown

29         interval: 1000

30         repeat: true

31         triggeredOnStart: true

32         onTriggered: {

33             countShow.text = attrs.count;

34             attrs.count -= 1;

35             if (attrs.count < 0){

36                 countDown.stop();

37                 countShow.text = "Clap Now!";

38                 startBtn.text = "Start"

39             }

40         }

41     }

42 

43     Button {

44         id: startBtn

45         text: "Start"

46         onClicked: {

47             attrs.count = 10;

48             countDown.start();

49             startBtn.text = "Restart"

50         }

51 

52         anchors.horizontalCenter: parent.horizontalCenter

53         anchors.bottom: parent.bottom

54         anchors.bottomMargin: 20

55     }

56 }

 

你可能感兴趣的:(定时器)