首页 > 其他 > 详细

属性绑定与赋值

时间:2015-06-01 16:07:45      阅读:166      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!