How to connect a broken signal of a C ++ object from QML?

I want to connect a broken C ++ signal QObjectfrom QML, so I did this:

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"
    }
}

But nothing is printed when I destroy qobj.

+4
source share
1 answer

In general, you can connect to signals emitted from a C ++ object using the Connections element :

Connections {
    target: yourObjectComingFromCpp
    onSomeSignal: console.log("Something")
}

or in Javascript by calling the function connectin the corresponding property of the JS-mapped object:

// without the *on*!
yourObjectComingFromCpp.someSignal.connect( /* JS function here */ );

However : this does not work for specific signals QObject::destroyedthat are forcibly blacklisted and are never available in QML ( source ).

, , QML, , QObject, , .

+5

All Articles