C

Loader QML Type

Allows dynamic loading of a subtree from a URL. More...

Import Statement: import QtQuick
Since: Qt Quick Ultralite 2.3

Properties

Methods

Detailed Description

Loader can load a pre-compiled QML component (using the source 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 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.15

Item {
    width: 200; height: 200
    Loader { id: pageLoader }

    MouseArea {
        anchors.fill: parent
        onClicked: pageLoader.source = "Page1.qml"
    }
}

Allocation

The Qt Quick Ultralite Loader allocates its items using the QmlDynamicObjects memory allocator.

The allocation happens each time the source changes. The deallocation happens before each allocation, and when the active property changes to false.

Note that all allocations in Qt Quick Ultralite are performed on a single thread. Depending on the duration of user-defined initialization routines (class constructors), short UI freezes may occur.

Loader Sizing Behavior

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

SizeLoader.qmlSizeItem.qmlMyRect.qml
import QtQuick 2.15

Item {
    width: 200; height: 200

    Loader {
        // Explicitly set the size of the
        // Loader to the parent item's size
        anchors.fill: parent
        source: "MyRect.qml"
    }
}
import QtQuick 2.15

Item {
    width: 200; height: 200

    Loader {
        // position the Loader in the center
        // of the parent
        anchors.centerIn: parent
        source: "MyRect.qml"
    }
}
import QtQuick 2.15

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.

Limitations

Behavioral and functional differences

Communication with the loaded items

Unlike in Qt Quick Loader, Qt Quick Ultralite Loader does not support accessing loaded items using the item property due to the lack of object introspection system.

This limitation effectively disables interactions (reading/writing properties or calling functions using Loader.item).

To work around this limitation, one can create a mediator singleton. For example:

import QtQuick 2.15
pragma singleton

QtObject {
    property string text: ""
}

This singleton is then used in the main.qml file:

import QtQuick 2.15
import Mediator

Column {
    Text {
        text: "Text from loaded item: " + Mediator.text
    }

    Loader {
        source: "MyText.qml"
    }
}

And the MyText.qml:

import QtQuick 2.15
import Mediator

Text {
    text: "mytext"
    Component.onCompleted: Mediator.text = text
}

View Delegates

The Qt Quick Ultralite Loader cannot be used within a View Delegate.

See also Known Issues or Limitations.

Property Documentation

active : bool

This property is true if the Loader is currently active. It is true by default.

If the Loader is inactive, changing the source will not instantiate the items until the Loader is active.

Setting the value to false, releases all the items loaded by the loader, but it does not affect the source.


item : ItemBase*

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

See also Communication with the loaded items.


source : string

This property holds the URL of the QML item to instantiate.

Loader can load visual items (those that inherit from Item) only.

Setting this property to either an empty string or a new URL, unloads the item that is loaded from the previous URL.


Method Documentation

void setSource(string source)

Creates an object instance of the given source component. The instance will be accessible using 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 will be cached. When the loader is made active, an instance of the source component will be created.

See also Communication with the loaded items.


Available under certain Qt licenses.
Find out more.