QQmlComponent Class

The QQmlComponent class encapsulates a QML component definition. More...

Header: #include <QQmlComponent>
qmake: QT += qml
Since: Qt 5.0
Instantiated By: Component
Inherits: QObject

This class was introduced in Qt 5.0.

Public Types

enum CompilationMode { PreferSynchronous, Asynchronous }
enum Status { Null, Ready, Loading, Error }

Properties

Public Functions

qreal progress() const
QQmlComponent::Status status() const
QUrl url() const

Signals

void progressChanged(qreal)
void statusChanged(QQmlComponent::Status)

Detailed Description

Components are reusable, encapsulated QML types with well-defined interfaces.

A QQmlComponent instance can be created from a QML file. For example, if there is a main.qml file like this:

 import QtQuick 2.0

 Item {
     width: 200
     height: 200
 }

The following code loads this QML file as a component, creates an instance of this component using create(), and then queries the Item's width value:

 QQmlEngine *engine = new QQmlEngine;
 QQmlComponent component(engine, QUrl::fromLocalFile("main.qml"));

 QObject *myObject = component.create();
 QQuickItem *item = qobject_cast<QQuickItem*>(myObject);
 int width = item->width();  // width = 200

To create instances of a component in code where a QQmlEngine instance is not available, you can use qmlContext() or qmlEngine(). For example, in the scenario below, child items are being created within a QQuickItem subclass:

 void MyCppItem::init()
 {
     QQmlEngine *engine = qmlEngine(this);
     // Or:
     // QQmlEngine *engine = qmlContext(this)->engine();
     QQmlComponent component(engine, QUrl::fromLocalFile("MyItem.qml"));
     QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
     childItem->setParentItem(this);
 }

Note that these functions will return null when called inside the constructor of a QObject subclass, as the instance will not yet have a context nor engine.

Network Components

If the URL passed to QQmlComponent is a network resource, or if the QML document references a network resource, the QQmlComponent has to fetch the network data before it is able to create objects. In this case, the QQmlComponent will have a Loading status. An application will have to wait until the component is Ready before calling QQmlComponent::create().

The following example shows how to load a QML file from a network resource. After creating the QQmlComponent, it tests whether the component is loading. If it is, it connects to the QQmlComponent::statusChanged() signal and otherwise calls the continueLoading() method directly. Note that QQmlComponent::isLoading() may be false for a network component if the component has been cached and is ready immediately.

 MyApplication::MyApplication()
 {
     // ...
     component = new QQmlComponent(engine, QUrl("http://www.example.com/main.qml"));
     if (component->isLoading())
         QObject::connect(component, SIGNAL(statusChanged(QQmlComponent::Status)),
                          this, SLOT(continueLoading()));
     else
         continueLoading();
 }

 void MyApplication::continueLoading()
 {
     if (component->isError()) {
         qWarning() << component->errors();
     } else {
         QObject *myObject = component->create();
     }
 }

Member Type Documentation

enum QQmlComponent::CompilationMode

Specifies whether the QQmlComponent should load the component immediately, or asynchonously.

ConstantValueDescription
QQmlComponent::PreferSynchronous0Prefer loading/compiling the component immediately, blocking the thread. This is not always possible; for example, remote URLs will always load asynchronously.
QQmlComponent::Asynchronous1Load/compile the component in a background thread.

enum QQmlComponent::Status

Specifies the loading status of the QQmlComponent.

ConstantValueDescription
QQmlComponent::Null0This QQmlComponent has no data. Call loadUrl() or setData() to add QML content.
QQmlComponent::Ready1This QQmlComponent is ready and create() may be called.
QQmlComponent::Loading2This QQmlComponent is loading network data.
QQmlComponent::Error3An error has occurred. Call errors() to retrieve a list of errors.

Property Documentation

progress : const qreal

The progress of loading the component, from 0.0 (nothing loaded) to 1.0 (finished).

Access functions:

qreal progress() const

Notifier signal:

void progressChanged(qreal)

status : const Status

The component's current status.

Access functions:

QQmlComponent::Status status() const

Notifier signal:

void statusChanged(QQmlComponent::Status)

url : const QUrl

The component URL. This is the URL passed to either the constructor, or the loadUrl(), or setData() methods.

Access functions:

QUrl url() const