[QML] Connections元素介绍

一个Connections对象创建一个了一个QML信号的连接。在QML中,我们连接信号通常是用使用"on<Signal>"来处理的,如下所示:

  1. MouseArea { 
  2.      onClicked: { foo(...) } 
  3. }

复制代码

然而, 以下几种情况则无法通过"on<Signal>"来实现: 
1.针对某个信号需要多个处理时,也就是有多个槽关联到同一个信号上。 
2.在信号的发送者的范围外(这里可以理解为发送者的类定义之外)创建连接。 
3.连接的目标不在QML中。
 
这时就该Connections登场咯 

先举个例子,上面的代码用Connections就可以像下面这样写:

  1. MouseArea { 
  2.      Connections { 
  3.          onClicked: foo(...) 
  4.      } 
  5. }

而一般来说,我们都会这么写:

  1. MouseArea { 
  2.      id: area 
  3. ... 
  4. Connections { 
  5.      target: area 
  6.      onClicked: foo(...) 
  7. }

zhezhelin

Property Binding

属性绑定后会自动更新

Rectangle { 
    width: otherItem.width 
    height: otherItem.height 
}

Rectangle { 
    function calculateMyHeight() { 
        return Math.max(otherItem.height, thirdItem.height); 
    }

    anchors.centerIn: parent 
    width: Math.min(otherItem.width, 10) 
    height: calculateMyHeight() 
    color: { if (width > 10) "blue"; else "red" } 
}

Changing Bindings

Rectangle { 
    id: rectangle 
    width: otherItem.width 
    height: otherItem.height

    states: State { 
        name: "square" 
        PropertyChanges { 
            target: rectangle 
            width: otherItem.height 
        } 
    } 
}

Binding Element

Binding { 
    target: system 
    property: "brightness" 
    value: slider.value 
}

猜你喜欢

转载自blog.csdn.net/qingzhuyuxian/article/details/86487766
QML
今日推荐