LoaderThe Loader item allows dynamically loading an Item-based subtree from a URL or Component. More... Inherits Item Properties
Signals
Methods
Detailed DescriptionLoader is used to dynamically load visual QML components. For loading non-visual components, see Dynamic Object Management in QML. 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: import QtQuick 2.0 Item { width: 200; height: 200 Loader { id: pageLoader } MouseArea { anchors.fill: parent onClicked: pageLoader.source = "Page1.qml" } } The loaded item can be accessed using the item property. 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 item, freeing resources and leaving the Loader empty. Loader sizing behaviorLoader is like any other visual item and must be positioned and sized accordingly to become visible.
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 itemsAny signals emitted from the loaded item can be received using the Connections element. 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 eventsLoader is a focus scope. Its focus property must be set to true for any of its children to get the active focus. (See the focus documentation page 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. See also Dynamic Object Creation. Property DocumentationThis property is true if the Loader is currently active. The default value for the active property is true. If the Loader is inactive, changing the source or sourceComponent will not cause the item to be instantiated until the Loader is made active. Setting the value to inactive will cause any item loaded by the loader to be released, but will not affect the source or sourceComponent. The status of an inactive loader is always Null. See also source and sourceComponent. This property holds whether the component will be instantiated asynchronously. 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. To avoid seeing the items loading progressively set visible appropriately, e.g. Loader { source: "mycomponent.qml" asynchronous: true visible: status == Loader.Ready } Note that this property affects object instantiation only; it is unrelated to loading a component asynchronously via a network. This property holds the top-level item that is currently loaded. This property holds the progress of loading QML data from the network, from 0.0 (nothing loaded) to 1.0 (finished). Most QML files are quite small, so this value will rapidly change from 0 to 1. See also status. This property holds the URL of the QML component to instantiate. Note the QML component must be an Item-based component. The loader cannot load non-visual components. To unload the currently loaded item, 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. See also sourceComponent, status, and progress. This property holds the Component to instantiate. Item { Component { id: redSquare Rectangle { color: "red"; width: 10; height: 10 } } Loader { sourceComponent: redSquare } Loader { sourceComponent: redSquare; x: 10 } } To unload the currently loaded item, set this property to an empty string or undefined. This property holds the status of QML loading. It can be one of:
Use this status to provide an update or respond to the status change in some way. For example, you could:
Note that if the source is a local file, the status will initially be Ready (or Error). While there will be no onStatusChanged signal in that case, the onLoaded will still be invoked. See also progress. Signal DocumentationThis handler is called when the status becomes Loader.Ready, or on successful initial load. Method DocumentationCreates 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. Example:
|