动态连接到动态创建对象的信号 [英] Connect dynamically to signals of dynamically created objects
问题描述
标题可能有点混乱,但我的意图很简单:
我有一个 [Repeater/Instantiator]
可以创建任意委托的多个实例.我想对委托实例的属性(只有第一级,因此没有属性的属性)的所有更改做出反应,调用函数
function update(index, propertyName)
这似乎很容易,但我失败了.这是我的代码
<块引用>TestObj.qml
中继器{onItemAdded: {var 键 = Object.keys(item)控制台日志(键)for (var k = 0; k
<块引用>
main.qml:
导入 QtQuick 2.7导入 QtQuick.Controls 2.0导入 QtQml 2.2将 QtQuick.Controls 1.4 导入为 Ctrl进口 '.'导入test.js"作为测试应用程序窗口{身份证:根可见:真实最小宽度:500最小高度:500属性 var blub: []柱子 {间距:5测试对象{型号:5委托:行{间距:2属性 int p1: 0属性 int p2: 2属性 int p3: 4按钮 {文字:parent.p1onClicked:parent.p1++}按钮 {文字:parent.p2onClicked: parent.p2 *= 2}按钮 {文字:parent.p3onClicked: parent.p3 *= parent.p3}}}}}
它成功连接了某些东西,但我无法正确锁定 key
.无论我更改哪个属性,我总是得到信息,我在示例中更改了属性 p3
.
如何锁定密钥,以便在更改具有相应名称的属性时获得propertyName
?
一种可能的解决方案是,创建一个具有该功能的对象,然后只分配该功能
中继器{onItemAdded: {var 键 = Object.keys(item)for (var k = 0; k
但也许有更好的解决方案等待公开?
The title might be a bit confusing, but my intent is pretty simple:
I have a [Repeater/Instantiator]
which creates multiple instances of a arbitrary delegate. I want to react to all changes of the properties (only first-level, so no properties of properties) of the instances of the delegate, calling a function
function update(index, propertyName)
This seems to be easy, but I fail. This is my code
TestObj.qml
Repeater {
onItemAdded: {
var keys = Object.keys(item)
console.log(keys)
for (var k = 0; k < keys.length; k++) {
if (item[keys[k] + 'Changed'] !== undefined && keys[k] !== 'objectName') {
var key = keys[k]
var str = "function() { console.log('Property, " + key + " of " + index + " changed') }"
console.log(str)
item[key + 'Changed'].connect(function() { console.log('Property', key, 'of', index, 'changed') })
}
}
}
}
main.qml:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQml 2.2
import QtQuick.Controls 1.4 as Ctrl
import '.'
import 'test.js' as Test
ApplicationWindow {
id: root
visible: true
minimumWidth: 500
minimumHeight: 500
property var blub: []
Column {
spacing: 5
TestObj {
model: 5
delegate: Row {
spacing: 2
property int p1: 0
property int p2: 2
property int p3: 4
Button {
text: parent.p1
onClicked: parent.p1++
}
Button {
text: parent.p2
onClicked: parent.p2 *= 2
}
Button {
text: parent.p3
onClicked: parent.p3 *= parent.p3
}
}
}
}
}
It succesfully connects something, but I can't get the key
properly locked. Whichever property I change, I always get the info, I had changed property p3
in my example.
How can I lock the key, so that I get propertyName
when I change the the property with the corresponding name?
One possible solution is, to create an object that has the function, and then just assign this function
Repeater {
onItemAdded: {
var keys = Object.keys(item)
for (var k = 0; k < keys.length; k++) {
if (item[keys[k] + 'Changed'] !== undefined && keys[k] !== 'objectName') {
var key = keys[k]
var f = __funproto.createObject(this, { i: index, n: key })
item[key + 'Changed'].connect(f.fun)
}
}
}
}
Component {
id: __funproto
QtObject {
property int i
property string n
function fun() { /*do something with it!*/}
}
}
But maybe there is a even better solution waiting to be disclosed?
这篇关于动态连接到动态创建对象的信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!