QML property weak reference?
-
I created object with
Qt.createComponent
, saved reference to property and then I calleddestroy
for object inonClosed
signal handler.
And what I can not understand is why property becomes null after destroyed without manually setting tonull
.
I thought that property is "strong reference", so even after destroying it will hold reference to some kind of "zombie" until I set it tonull
manually, but looks like property is some kind of "weak reference"?ApplicationWindow { id: applicationWindow property MyPopup dlg: null function show() { const comp = Qt.createComponent("qrc:/MyPopup.qml"); const params = {parent: applicationWindow.contentItem}; applicationWindow.dlg = comp.createObject(params.parent, params); console.assert(applicationWindow.dlg !== null, "MyPopup creation failed"); applicationWindow.dlg.open(); }
Popup { onClosed: { console.log("onClosed was called", view); view.destroy(); }
-
@Evgeniy-Dushistov I had not noticed that previously, and there's no obvious mention of it in the documentation. Thanks! I see the same thing with this code:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { id: root property Rectangle myRect Component { id: myComponent Rectangle { color: "yellow" } } MouseArea { anchors.fill: parent onClicked: { if (loader.status == Loader.Null) { loader.sourceComponent = myComponent; myRect = loader.item; } else { loader.sourceComponent = null; } } } Loader { id: loader anchors.fill: parent } Text { anchors.centerIn: parent; text: "myRect: " + root.myRect } }
My guess is that the properties that hold a QObject are tracked with a QPointer, but I'm not familiar enough with the implementation to find the answer quickly.