[QML] Connections元素介绍

来源:互联网 发布:mac mini配4k显示器 编辑:程序博客网 时间:2024/05/16 07:43
一个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. ...
  5. Connections {
  6.      target: area
  7.      onClicked: foo(...)
  8. }


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
}



0 0