码迷,mamicode.com
首页 > 其他好文 > 详细

属性绑定与赋值

时间:2015-06-01 16:09:47      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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

 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 }

 

属性绑定与赋值

标签:

原文地址:http://www.cnblogs.com/xiaomanon/p/4543903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!