Qt.createComponent
only accepts a URL
as an argument, not a type name. Therefore, using Qt.createComponent(Type)
is not possible. The question then arises - what benefits could this restriction potentially provide?
- There is no flexibility offered, as QML does not allow passing around types as values.
- No performance advantage is gained either, since even with
Qt.createComponent(URL)
, the engine's component cache is still utilized.
Generally, explicit JS component creation using Qt.createComponent
is not typically the preferred approach in QML due to its declarative nature that allows most tasks to be accomplished in a declarative manner.
Alternative methods for creating components should be considered:
If a property's type is Component
, standard syntax can be used to create objects without instantiating them entirely:
property Component someProperty: Item {
// This creates a prototype/component, bound to the property 'someProperty'
}
Objects can also be wrapped in a Component
if full instantiation is not desired yet:
Component {
id: myComponent // Reference this Component later
Item {
// Create a prototype/component that can be referenced by the Component's ID
}
}
This technique can also be applied in property assignments:
property var someProperty: Component {
Item {
}
}
TL;DR
In QML, it is not possible to pass a type to a function, hence using Qt.createComponent
with a type is similarly restricted. If you feel there is a need for this capability and none of the mentioned techniques suffice, please provide more details about your specific requirements so a suitable solution can be found.