我想从 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 时没有打印任何内容。

最佳答案

在一般情况下,您可以使用 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 自己的析构函数中,这意味着您的子类上的任何属性或方法访问此时都是无效的。

关于javascript - 如何从 QML 连接 C++ 对象的销毁信号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30730932/

10-14 16:13
查看更多