QObject gets destroyed after being put into QML variable

穿精又带淫゛_ 提交于 2019-12-08 08:45:23

问题


Today I've faced strange bug in our program. The object of a class inherited from QObject was being deleted by event with type QEvent::DefferedDelete, while nobody could possibly send it.

It was passed into QML as QVariant:

// cpp: 
        Q_INVOKABLE QVariant currentDevice_v() const {
            return QVariant::fromValue(_current);
        }
// qml: 
       Component.onCompleted: {
            curDevice = devicesModel.currentDevice_v()
            #...
       }

Without that qml line everything worked well - nothing produces delete event.


回答1:


What I've figured out that if I set the parent of that QObject before I pass it into QML, then it doesn't get deleted. So, I've concluded that passing unparented QObject into QML scope makes that scope become a parent of QObject and call its destructor after scope ends.

Sharing this out, as I haven't found an answer anywhere. But while writing this post I've found similar unanswered issue: Qt5.6 QML, why are dynamic models destroyed after garbage collection?



来源:https://stackoverflow.com/questions/42254141/qobject-gets-destroyed-after-being-put-into-qml-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!