I have a Loader
object that loads some very heavy components. Some event arrives in the middle of the load that requires loading to stop and go back to empty the Loader
. Is it possible?
Abort object creation
As documented by Qt, three methods exists to unload/abort an object instantiation:
Loader.active
to false
Loader.source
to an empty stringLoader.sourceComponent
to undefined
Asynchronous behaviour
To be able to change these properties during loading, Loader.asynchronous
should be true
, otherwise the GUI thread is busy with loading the object. You also need to QQmlIncubationController
for your QQmlEngine
to control the idle time used for object incubation. Without such a controller Loader.asynchronous
does not have any effect. Note that QQmlApplicationEngine
automatically installs a default controller if the scene contains a QQuickWindow
.
Bug: memory leak
Up to the last tested Qt version (Qt 5.8.0, 5.9.0 beta), a severe memory leaks exist when aborting an unfinished object incubation (at least in certain cases, including the example in the answer of derM) resulting in a fast memory usage increase for large components. A bug report is created including a proposed solution.
According to the bug report, this should be fixed in Qt version 5.15 (not tested).