您不会从父节点连接到主节点,而是像这样:
...
id: idOfTheParent // <=== THIS IS IMPORTANT
signal update_values(new_values)
function qt_update_values(newValues){
update_values(newValues);
}
Repeater {
id:idRepeater
model: 3
Rectangle {
id:example
Text{ text: "hello"}
...
AnotherComponent {
id: idOfAnotherComponent // This ID is only available in the
// scope of the Component
// that will be instantiated by the
// Repeater, i.e. children of the Rectangle
name: "name"
othervariables: "others"
}
Connections {
target: idOfTheParent
onUpdate_values: idOfAnotherComponent.dosomethingWith(new_values)
}
}
}
...
您也可以使用signal.connect() 添加新连接
Repeater {
model: 10
delegate: Item { ... }
onItemAdded: {
idOfTheParent.update_values.connect(function() { // do what you want })
}
}
但如果它只是广播一个新值,声明式的方式是,在你的委托中拥有属性,并将它们绑定到保存变化值的属性:
...
id: idOfTheParent
property var valueThatWillChange
Repeater {
model: 10
delegate: Item {
property int valueThatShallChangeToo: idOfTheParent.valueThatWillChange
}
}
...
让所有这些都带有不同的信号。是可能的:
对于Connections-解决方案,最简单的方法是调用doSomething,前提是它是正确的委托实例:
// in the delegate
Connections {
target: idOfTheParent
onValue1Updated: if (index === 1) doYourStuff()
onValue2Updated: if (index === 2) doYourStuff()
onValue...
}
但是第二种方法更容易:
id: idOfTheParent
Repeater {
model: 10
delegate: SomeItem {
function doSomething() { console.log(index, 'does something')
}
onItemAdded: {
idOfTheParent['value' + index + 'Updated'].connect(item.doSomething)
}
onItemRemoved: {
idOfTheParent['value' + index + 'Updated'].disconnect(item.doSomething)
}
}