我想从QML连接一个被破坏的C ++ QObject
信号,所以我这样做了:
Rectangle
{
id: root
width: 128
height: 128
Button
{
anchors.centerIn: parent
text: "Click me"
onClicked:
{
qobj.Component.onDestruction.connect(function(){console.log("It destroy")}) // qobj is set from c++
qobj.destroy() // should output "It destroy"
}
}
但是当我摧毁qobj
时没有打印出来。
答案 0 :(得分:5)
在一般情况下,您可以使用Connections元素连接到C ++对象发出的信号:
Connections {
target: yourObjectComingFromCpp
onSomeSignal: console.log("Something")
}
或在Javascript中通过调用JS映射对象的相应属性上的connect
函数:
// without the *on*!
yourObjectComingFromCpp.someSignal.connect( /* JS function here */ );
然而:这不适用于特定的QObject::destroyed
信号,这些信号被强制列入黑名单,并且在QML(source)中永远不可用。
我想原因是该对象无论如何都会从QML上下文消失,再加上当发出该信号时,你深入到QObject自己的析构函数中,这意味着子类上的任何属性或方法访问都是无效的那一点。