Get notified that object gonna be destroyed

  • I have QObject-derived class "Banner" exposed to QML as QML_ELEMENT and QML_UNCREATEBLE,
    at QML side i have poroperty of type Banner

    property Banner myBanner: BannerFactory.createBanner()

    if later, somewhere in QML i call myBanner.destroy() that will lead to call of Banner destructor ~Banner::Banner(), but is it possible to get notified inside Banner class that object going to be destroyed before destructor is called?

  • @morte
    Slightly strange request, why should you need to know before destructor is called?

    Anyway, I don't know about QML, but Qt has QObject::destroyed signal for every QObject:

    This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.

    All the objects's children are destroyed immediately after this signal is emitted.

    I presume that will come before destructor. So maybe you can slot onto that for your purposes.

  • I want to do some cleanup actions for external resources in separate thread that is problematic to in destructor itself.

    QObject::destroyed emmited after ~Banner::Banner destructor, i.e. :
    1)QML: banner.destroy()
    2)C++: ~Banner::Banner()
    3)C++: emit QObject::destroyed()
    4)C++: ~QObject::QObject()

  • @morte
    If I understand correctly: I don't know what myObject is. But if you want to be notified of Banner's destruction you are supposed to slot onto Banner::destroyed() rather than myObject::destroyed(), if that is what you are doing.

Log in to reply