Properties

Signals

Methods

Detailed Description

Loader is used to dynamically load QML components.

Loader can load a QML file (using the source property) or a Component object (using the sourceComponent property). It is useful for delaying the creation of a component until it is required: for example, when a component should be created on demand, or when a component should not be created unnecessarily for performance reasons.

Here is a Loader that loads "Page1.qml" as a component when the MouseArea is clicked:

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.

Loader Sizing Behavior

If the source component is not an Item type, Loader does not apply any special sizing rules. When used to load visual types, Loader applies the following sizing rules:

If an explicit size is not specified for the Loader, the Loader is automatically resized to the size of the loaded item once the component is loaded.

If the size of the Loader is specified explicitly by setting the width, height or by anchoring, the loaded item will be resized to the size of the Loader.

In both scenarios the size of the item and the Loader are identical. This ensures that anchoring to the Loader is equivalent to anchoring to the loaded item.

Receiving Signals from Loaded Objects

Any signals emitted from the loaded object can be received using the Connections type. For example, the following application.qml loads MyItem.qml, and is able to receive the message signal from the loaded item through a Connections object:

Alternatively, since MyItem.qml is loaded within the scope of the Loader, it could also directly call any function defined in the Loader or its parent Item.

Focus and Key Events

Loader is a focus scope. Its focus property must be set to true for any of its children to get the active focus. (See Keyboard Focus in Qt Quick for more details.) Any key events received in the loaded item should likely also be accepted so they are not propagated to the Loader.

For example, the following application.qml loads KeyReader.qml when the MouseArea is clicked. Notice the focus property is set to true for the Loader as well as the Item in the dynamically loaded object:

Once KeyReader.qml is loaded, it accepts key events and sets event.accepted to true so that the event is not propagated to the parent Rectangle.

Since QtQuick 2.0, Loader can also load non-visual components.

Using a Loader within a View Delegate

In some cases you may wish to use a Loader within a view delegate to improve delegate loading performance. This works well in most cases, but there is one important issue to be aware of related to the creation context of a Component.

In the following example, the index context property inserted by the ListView into delegateComponent's context will be inaccessible to Text, as the Loader will use the creation context of myComponent as the parent context when instantiating it, and index does not refer to anything within that context chain.

This property holds whether the component will be instantiated asynchronously. By default it is false.

When used in conjunction with the source property, loading and compilation will also be performed in a background thread.

Loading asynchronously creates the objects declared by the component across multiple frames, and reduces the likelihood of glitches in animation. When loading asynchronously the status will change to Loader.Loading. Once the entire component has been created, the item will be available and the status will change to Loader.Ready.

Changing the value of this property to false while an asynchronous load is in progress will force immediate, synchronous completion. This allows beginning an asynchronous load and then forcing completion if the Loader content must be accessed before the asynchronous load has completed.

Since QtQuick 2.0, Loader is able to load any type of object; it is not restricted to Item types.

To unload the currently loaded object, set this property to an empty string, or set sourceComponent to undefined. Setting source to a new URL will also cause the item created by the previous URL to be unloaded.

Method Documentation

Creates an object instance of the given source component that will have the given properties. The properties argument is optional. The instance will be accessible via the item property once loading and instantiation is complete.

If the active property is false at the time when this function is called, the given source component will not be loaded but the source and initial properties will be cached. When the loader is made active, an instance of the source component will be created with the initial properties set.

Setting the initial property values of an instance of a component in this manner will not trigger any associated Behaviors.

Note that the cached properties will be cleared if the source or sourceComponent is changed after calling this function but prior to setting the loader active.