Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
When do the Loader clean resource?
reiko last edited by
I use Qt5.3 with QtQuick2.0.
First, I use the Loader to load "screen1.qml". screen1.qml just has a Rectangle element with objectName = "screen1".
Then, when the user click the Rectangle on the screen1.qml, I change the Loader.source to "screen2.qml".
screen2.qml just has a Rectangle element with objectName = "screen2", too.
On the Loader::onLoaded (), I connect a single to a c++ slot.
In the c++ slot, I use QObject::findChild() to see if child object "screen1" and "screen2" could be found.
When the Loader.source sets to "screen2.qml", and the Loader::onLoaded () is called, the slot still shows that screen1 and screen2 could be found!!
Why the screen1 could be found? I has already changed the Loader.source to "screen2.qml"!
In the QT document, it writes "If the source or sourceComponent changes, any previously instantiated items are destroyed."
But if I sets the "asynchronous" property of the Loader to "true", everything is OK!! (the slot just shows that screen2 could be found, no screen1 )
I am confused....
(sorry for my poor english)
Gianluca last edited by
on QML the objects are not destroyed immediately, but there is something similar to a garbage collector as a concept.
So, when you change from screen1 to screen2 the object is not destroyed at the same time, but it's delete later (means that an event of deleting is emitted and when the app process the events will eventually destroy screen1 if no other object are using it).
So, in both case the screen1 will be destroyed, but this happen in a different order and that results in something strange from your point of view.
If you want to check, put a Timer that every 500 ms print out the child of your Loader object and you can see that the screen1 will be destroyed in any case.