QML Loader ... How to unload or delete a qml page loaded on to Loader element
Asked Answered
L

4

7

I have loaded MyItem.qml as a source component onto Loader element. Now i want to remove or unload that page from Loader element. I tried to set source : "" & sourceComponent : "" , also sourceComponent : "undefined". But it did not work

Logging answered 15/2, 2017 at 15:42 Comment(1)
Can you show us some code, where you tried it (mcve)?Duran
M
7

You should set sourceComponent = undefined or source = "". Usually, I use this code:

Loader{
    id: loader
    function show(component) {
        sourceComponent = component;
    }
    function hide(){
        sourceComponent = undefined;
    }
}
Mantissa answered 27/7, 2017 at 14:27 Comment(1)
The documentation clearly say that you need to specify undefined instead of null, still, this solved the issue for me :)Americano
A
4

You can just set the active property to false for unload or true (default) for loading.

Auberon answered 16/2, 2017 at 9:11 Comment(1)
Such a simple and concise solution. This has become my preferred way.Nimbus
D
0

You didn't say what the actual issue was, but in case you ran into errors where the component you're trying to unload is trying to access things it shouldn't, it could be because Loader deletes the item using deleteLater(), which means it doesn't get deleted instantly. See this bug report for more info:

https://bugreports.qt.io/browse/QTBUG-60344

Deloisedelong answered 28/7, 2017 at 7:25 Comment(0)
M
0

For example there is this loader:

Loader {
    id: loaderLanguage
}

to inactive:

loaderLanguage.active= false
Maltz answered 5/9, 2022 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.