I want to dynamically create and destroy Item described in myComponent (actually, in real world, I want to do this with many items, and I need to manage them, that is why I use ListModel in the example below)
So here is minimal example, in which all works: as expected
import QtQuick 2.2
Item {
id: root
property ListModel itemsModel: ListModel { }
Component {
id: myComponent
Item {
Component.onCompleted: console.log("Component.onCompleted")
Component.onDestruction: console.log("Component.onDestruction")
}
}
Component {
id: loaderComponent
Loader { id: loader; sourceComponent: myComponent; }
}
Component.onCompleted: {
var loader = loaderComponent.createObject(root)
// v1 ========================= WORKS =========================
itemsModel.append({tab: loader})
var temp = itemsModel.get(0).tab
temp.sourceComponent = undefined
itemsModel.clear()
// v2 ========================= DOES NOT WORK =========================
// itemsModel.append(loader)
// var temp = itemsModel.get(0)
// temp.sourceComponent = undefined
// itemsModel.clear()
}
}
It gives following output:
Component.onCompleted
Component.onDestruction
all exactly as expected.
But, if you comment v1 and uncomment v2, you will not see
Component.onDestruction
Output will be only this:
Component.onCompleted
So, why in v2 destructor of Item is not called (regardless of temp.sourceComponent = undefined and according to this [qt-project.org] it should be called:
If the source or sourceComponent changes, any previously instantiated items are destroyed. Setting source to an empty string or setting sourceComponent to undefined destroys the currently loaded object, freeing resources and leaving the Loader empty.
)?
↧