一个Connections对象创建一个了一个QML信号的连接。在QML中,我们连接信号通常是用使用"on<Signal>"来处理的,如下所示:
- MouseArea {
- onClicked: { foo(...) }
- }
复制代码
然而, 以下几种情况则无法通过"on<Signal>"来实现:
1.针对某个信号需要多个处理时,也就是有多个槽关联到同一个信号上。
2.在信号的发送者的范围外(这里可以理解为发送者的类定义之外)创建连接。
3.连接的目标不在QML中。
这时就该Connections登场咯
先举个例子,上面的代码用Connections就可以像下面这样写:
- MouseArea {
- Connections {
- onClicked: foo(...)
- }
- }
而一般来说,我们都会这么写:
- MouseArea {
- id: area
- }
- ...
- Connections {
- target: area
- onClicked: foo(...)
- }
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
}