属性绑定与赋值

首先看下面一段示例代码:

 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: 320

 9     height: 320

10     color: "red"

11     opacity: 0.3

12 

13     Text {

14         id: label

15         x: 20; y: 20

16 

17         property  int spacePresses: 0

18         text: "space pressed: " + spacePresses + " times"

19 

20         onTextChanged: console.log("text changed to: ", text)

21 

22         focus: true

23         Keys.onPressed: {

24             increment()

25         }

26 

27         Keys.onEscapePressed: {

28             label.text = ''

29         }

30 

31         function increment() {

32             spacePresses += 1

33         }

34     }

35 }

 当前应用不大适合用属性绑定来解决,使用赋值更合适:

 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: 320

 9     height: 320

10     color: "red"

11     opacity: 0.3

12 

13     Text {

14         id: label

15         x: 20; y: 20

16 

17         property  int spacePresses: 0

18         text: "space pressed: " + spacePresses + " times"

19 

20         onTextChanged: console.log("text changed to: ", text)

21 

22         focus: true

23         Keys.onSpacePressed: {

24             increment()

25             text = "space pressed: " + spacePresses + " times"

26         }

27 

28         Keys.onEscapePressed: {

29             text = "Release Binding: " + spacePresses

30         }

31 

32         function increment() {

33             spacePresses += 1

34         }

35     }

36 }

 

你可能感兴趣的:(属性)