• 属性绑定与赋值


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

     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      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      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 }

    关于属性绑定有一条需要特别注意:

          我们可以通过在属性的初始化时将属性(被绑定属性)绑定到其他的属性(关联属性)上,这样此属性就能够随着绑定的属性的变化而变化。但是,如果我们在其他地方对被绑定属性进行了赋值操作,那么它和关联属性之间的绑定就失效了,因此在编写代码的时候需要格外小心。

  • 相关阅读:
    君のことが好きだよ。
    [拓展Bsgs] Clever
    同余方程笔记
    [HAOI2008] 糖果传递
    [USACO10DEC] Treasure Chest
    [APIO2007] 风铃
    Luogu_2015 二叉苹果树
    关于高精度
    关于博弈论
    关于DP和背包
  • 原文地址:https://www.cnblogs.com/xiaomanon/p/4543903.html
Copyright © 2020-2023  润新知