可以动态连接到动态创建的项目的“破坏信号”
myOBJ
是以下组件:
Item {
id:root
signal foo()
property string bar: "bar"
Component.onCompleted: root.foo()
}
通过 qt.CreateComponent(...)
- > comp.CreateObject(...)
可以将JS函数连接到所有信号,除 destruction
以下代码:
var comp = Qt.createComponent('MyObj.qml');
var finish = () => {
if(comp.status === Component.Error) {
console.log("Error loading component:", comp.errorString())
return
}
if(comp.status !== Component.Ready) {
console.log("Component not ready")
return
}
var obj = comp.createObject(mainWindow, {})
if(obj === null) {
console.log('Error creating object')
return
}
obj.foo.connect(() => console.log('foo!'))
obj.barChanged.connect(() => console.log('barChanged!'))
obj.destruction.connect(() => console.log('destruction!'))
}
if(comp.status !== Component.Loading)
finish();
else
comp.statusChanged.connect(finish);
产生错误:
qrc:/main.qml:32:typeError:无法调用未定义的方法“连接”
与obj.desteruction.connect(...)
文档没有提及任何这样的限制。
怎么了?
放置:
Component.onDestruction: console.log("#destruction")
在中,MyOBJ
照常工作,但这不是我需要的。
MyObj
is the following component:
Item {
id:root
signal foo()
property string bar: "bar"
Component.onCompleted: root.foo()
}
When creating it dynamically via Qt.createComponent(...)
-> comp.createObject(...)
it is possible to connect JS functions to all signals, except destruction
Following code:
var comp = Qt.createComponent('MyObj.qml');
var finish = () => {
if(comp.status === Component.Error) {
console.log("Error loading component:", comp.errorString())
return
}
if(comp.status !== Component.Ready) {
console.log("Component not ready")
return
}
var obj = comp.createObject(mainWindow, {})
if(obj === null) {
console.log('Error creating object')
return
}
obj.foo.connect(() => console.log('foo!'))
obj.barChanged.connect(() => console.log('barChanged!'))
obj.destruction.connect(() => console.log('destruction!'))
}
if(comp.status !== Component.Loading)
finish();
else
comp.statusChanged.connect(finish);
produces the error:
qrc:/main.qml:32: TypeError: Cannot call method 'connect' of undefined
exactly at the line with obj.destruction.connect(...)
The documentation doesn't mention any such restriction.
What's wrong?
Putting:
Component.onDestruction: console.log("#destruction")
in MyObj
works as usual, but that's not what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有意识到JS需要与QML相同的语法来访问超类的属性。
以下作用很好:
I didn't realize JS needs the same syntax as QML for accessing properties of the superclass.
The following works just fine: