Qt Reference Documentation

Contents

QML Loader Element

The Loader item allows dynamically loading an Item-based subtree from a URL or Component. More...

Inherits Item

This element was introduced in Qt 4.7.

Properties

Signal Handlers

Detailed Description

Loader is used to dynamically load visual QML components. It 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 1.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 behavior

Loader 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.

sizeloader.qmlsizeitem.qml
 import QtQuick 1.0

 Item {
     width: 200; height: 200

     Loader {
         // Explicitly set the size of the Loader to the parent item's size
         anchors.fill: parent
         sourceComponent: rect
     }

     Component {
         id: rect
         Rectangle {
             width: 50
             height: 50
             color: "red"
         }
     }
 }
 import QtQuick 1.0

 Item {
     width: 200; height: 200

     Loader {
         // position the Loader in the center of the parent
         anchors.centerIn: parent
         sourceComponent: rect
     }

     Component {
         id: rect
         Rectangle {
             width: 50
             height: 50
             color: "red"
         }
     }
 }
The red rectangle will be sized to the size of the root item.The red rectangle will be 50x50, centered in the root item.

Receiving signals from loaded items

Any 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:

application.qmlMyItem.qml
 import QtQuick 1.0

 Item {
     width: 100; height: 100

     Loader {
        id: myLoader
        source: "MyItem.qml"
     }

     Connections {
         target: myLoader.item
         onMessage: console.log(msg)
     }
 }
 import QtQuick 1.0

 Rectangle {
    id: myItem
    signal message(string msg)

    width: 100; height: 100

    MouseArea {
        anchors.fill: parent
        onClicked: myItem.message("clicked!")
    }
 }

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 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:

application.qmlKeyReader.qml
 import QtQuick 1.0

 Rectangle {
     width: 200; height: 200

     Loader {
         id: loader
         focus: true
     }

     MouseArea {
         anchors.fill: parent
         onClicked: loader.source = "KeyReader.qml"
     }

     Keys.onPressed: {
         console.log("Captured:", event.text);
     }
 }
 import QtQuick 1.0

 Item {
     Item {
         focus: true
         Keys.onPressed: {
             console.log("Loaded item captured:", event.text);
             event.accepted = true;
         }
     }
 }

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 Documentation

read-onlyitem : Item

This property holds the top-level item that is currently loaded.


read-onlyprogress : real

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.


source : url

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.


sourceComponent : Component

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.

See also source and progress.


read-onlystatus : enumeration

This property holds the status of QML loading. It can be one of:

  • Loader.Null - no QML source has been set
  • Loader.Ready - the QML source has been loaded
  • Loader.Loading - the QML source is currently being loaded
  • Loader.Error - an error occurred while loading the QML source

Use this status to provide an update or respond to the status change in some way. For example, you could:

  • Trigger a state change:
     State { name: 'loaded'; when: loader.status == Loader.Ready }
  • Implement an onStatusChanged signal handler:
     Loader {
         id: loader
         onStatusChanged: if (loader.status == Loader.Ready) console.log('Loaded')
     }
  • Bind to the status value:
     Text { text: loader.status == Loader.Ready ? 'Loaded' : 'Not loaded' }

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 Handler Documentation

Loader::onLoaded ()

This handler is called when the status becomes Loader.Ready, or on successful initial load.