Zz *_*Tux 4 javascript qt qml qtquick2
我想QObject从QML 连接一个被破坏的C++信号,所以我这样做了:
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"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我破坏没有打印qobj.
在一般情况下,您可以使用Connections元素连接到C++对象发出的信号:
Connections {
target: yourObjectComingFromCpp
onSomeSignal: console.log("Something")
}
Run Code Online (Sandbox Code Playgroud)
或者在Javascript中通过调用connectJS映射对象的相应属性上的函数:
// without the *on*!
yourObjectComingFromCpp.someSignal.connect( /* JS function here */ );
Run Code Online (Sandbox Code Playgroud)
但是:这不适用于特定QObject::destroyed信号,这些信号被强制列入黑名单,并且在QML(源)中永远不可用.
我想原因是该对象无论如何都会从QML上下文中消失,而且当发出该信号时,您深入到QObject自己的析构函数中,这意味着您的子类上的任何属性或方法访问在该点都是无效的.