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

前端 未结 1 851
我寻月下人不归
我寻月下人不归 2020-12-21 01:53

I want to connect a destroyed signal of C++ QObject from QML so I did this:

Rectangle
{
    id: root
    width: 128
    height: 128

Button
{
           


        
相关标签:
1条回答
  • 2020-12-21 01:58

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

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

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

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

    However: this doesn't work for the specific QObject::destroyed signals, which are forcibly blacklisted and never available in QML (source).

    I guess the reason is that the object is gone from the QML context anyhow at that point, plus when that signal is emitted you're deep into QObject's own destructor, which means any property or method access on your subclass is invalid at that point.

    0 讨论(0)
提交回复
热议问题