Both can create objects dynamically.
When should Loader
be preferred over Qt.createQmlObject
and vice versa in QML?
The Loader
can be regarded as a placeholder for a particular object. It also gives you the ability to reference the underlying object through the Loader
's id.
Qt.createQmlObject
is generally more powerful than a Loader
, because you can instantiate as many objects as you want, and also it doesn't have the overhead of the Loader
. But you have to take care to keep track of what you create in order to be able to reference it.
The other functions Qt.createComponent()
and then createObject()
offer similar advantages, plus the possibility to pass properties to be used in the object's creation instead of setting them only after it has been created.
I personally see very little point in Loader
and rarely use it, if at all in production code. IMO it was introduced for the sake of the "non-programmer" much like most of the recent development, such as the new designer and QML .ui
files, which I find kind of annoying, but it is understandable - trying to increase adoption by non-programmers.