Loader Element Reference

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

[Inherits Item]

This element was introduced in Qt 4.7.

Detailed Description

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

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:

  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     width: 200; height: 200
  5.  
  6.     Loader  { id: pageLoader }
  7.  
  8.     MouseArea  {
  9.         anchors.fill: parent
  10.         onClicked: pageLoader.source = "Page1.qml"
  11.     }
  12. }

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.

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

sizeloader.qmlsizeitem.qml
  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     width: 200; height: 200
  5.  
  6.     Loader  {
  7.         // Explicitly set the size of the Loader to the parent item's size
  8.         anchors.fill: parent
  9.         sourceComponent: rect
  10.     }
  11.  
  12.     Component  {
  13.         id: rect
  14.         Rectangle  {
  15.             width: 50
  16.             height: 50
  17.             color: "red"
  18.         }
  19.     }
  20. }
  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     width: 200; height: 200
  5.  
  6.     Loader  {
  7.         // position the Loader in the center of the parent
  8.         anchors.centerIn: parent
  9.         sourceComponent: rect
  10.     }
  11.  
  12.     Component  {
  13.         id: rect
  14.         Rectangle  {
  15.             width: 50
  16.             height: 50
  17.             color: "red"
  18.         }
  19.     }
  20. }
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
  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     width: 100; height: 100
  5.  
  6.     Loader  {
  7.        id: myLoader
  8.        source: "MyItem.qml"
  9.     }
  10.  
  11.     Connections  {
  12.         target: myLoader.item
  13.         onMessage: console.log(msg)
  14.     }
  15. }
  1. import QtQuick 1.0
  2.  
  3. Rectangle  {
  4.    id: myItem
  5.    signal message(string msg)
  6.  
  7.    width: 100; height: 100
  8.  
  9.    MouseArea  {
  10.        anchors.fill: parent
  11.        onClicked: myItem.message("clicked!")
  12.    }
  13. }

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
  1. import QtQuick 1.0
  2.  
  3. Rectangle  {
  4.     width: 200; height: 200
  5.  
  6.     Loader  {
  7.         id: loader
  8.         focus: true
  9.     }
  10.  
  11.     MouseArea  {
  12.         anchors.fill: parent
  13.         onClicked: loader.source = "KeyReader.qml"
  14.     }
  15.  
  16.     Keys.onPressed:  {
  17.         console.log("Captured:", event.text);
  18.     }
  19. }
  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     Item  {
  5.         focus: true
  6.         Keys.onPressed:  {
  7.             console.log("Loaded item captured:", event.text);
  8.             event.accepted = true;
  9.         }
  10.     }
  11. }

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.

Properties

Signal Handlers

Property Documentation

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.

  1.                 Item  {
  2.     Component  {
  3.         id: redSquare
  4.         Rectangle  { color: "red"; width: 10; height: 10 }
  5.     }
  6.  
  7.     Loader  { sourceComponent: redSquare }
  8.     Loader  { sourceComponent: redSquare; x: 10 }
  9. }

To unload the currently loaded item, set this property to an empty string or undefined.

See also source and progress.

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:
    1.                         State  { name: 'loaded'; when: loader.status == Loader.Ready }
  • Implement an onStatusChanged signal handler:
    1.                         Loader  {
    2.     id: loader
    3.     onStatusChanged: if (loader.status == Loader.Ready) console.log('Loaded')
    4. }
  • Bind to the status value:
    1.                         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.

Notes provided by the Qt Community
Cool Hack
  • 4

Votes: 1

Coverage: Qt library 4.7, Qt 4.8

Picture of sierdzio sierdzio

Area 51 Engineer
3 notes

A tip on how to locally (temporarily) disable QRC URL parsing

As with the whole QML, once you use QRC (Qt Resource System) somewhere, it will automatically use “qrc:/” scheme in all subsequent uses. This is very useful for most use cases, but sometimes you might want to load a non-qrc file in your Loader (or other element). I’ve created a nice hack that enables this in a neat way – you can use it any time, and nothing will break!

Here’s the link to wiki page: How to locally disable qrc in QML [qt-project.org]

[Revisions]

Informative
  • 0

Votes: 0

Coverage: Qt library 4.7, 4.8

Picture of owenzhao owenzhao

Lab Rat
1 note

You need to set dialog close if you use a timer to change the loader.source

There are 3 qml files. The app will lost focus when you set dialogLoader.source = “” in dialog02.qml. You need to call dialog.close() in the timer in dialog01.qml. I file a bug of this as I think this should not be necessary. But it is what works currently.
file01.qml,

  1. import QtQuick 1.1
  2. import com.nokia.symbian 1.1
  3.  
  4. Page {
  5.     id: mainPage
  6.     Loader { id: dialogLoader }
  7.  
  8.     Button {
  9.         anchors.centerIn: parent
  10.         text: "press me"
  11.         onClicked: dialogLoader.source = "dialog01.qml"
  12.     }
  13. }

dialog01.qml

  1. import QtQuick 1.1
  2. import com.nokia.symbian 1.1
  3.  
  4. Item {
  5.     CommonDialog { id: dialog
  6.         Component.onCompleted: {
  7.             dialog.open()
  8.             myTimer.start()
  9.         }
  10.         //buttonTexts: ["don't press me, wait the timer close me"]
  11.         titleText: "dialog 01"
  12.         content: Text {
  13.             anchors.centerIn: parent
  14.             width: 300
  15.             wrapMode: Text.WordWrap
  16.             color: "white"
  17.             text: "just wait the timer close me in 5 seconds"
  18.         }
  19.         //onButtonClicked: dialogLoader.source = "dialog02.qml"
  20.     }
  21.     Timer {id: myTimer
  22.         interval: 5000
  23.         onTriggered: {
  24.             //dialog.close() //use this to get focus after loader.source set to ""
  25.             dialogLoader.source = "dialog02.qml"
  26.         }
  27.     }
  28.  
  29. }

dialog02.qml

  1. import QtQuick 1.1
  2. import com.nokia.symbian 1.1
  3.  
  4. Item {
  5.     CommonDialog { id: dialog
  6.         Component.onCompleted: dialog.open()
  7.         buttonTexts: ["press me"]
  8.         titleText: "dialog 02"
  9.         onButtonClicked: dialogLoader.source = ""
  10.     }
  11. }

[Revisions]