QQmlComponent#

The QQmlComponent class encapsulates a QML component definition. More

Inheritance diagram of PySide6.QtQml.QQmlComponent

Synopsis#

Properties#

Functions#

Virtual functions#

Slots#

Signals#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

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:

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 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 statusChanged() signal and otherwise calls the continueLoading() method directly. Note that 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, &QQmlComponent::statusChanged,
                         this, &MyApplication::continueLoading);
    } else {
        continueLoading();
    }
}

void MyApplication::continueLoading()
{
    if (component->isError()) {
        qWarning() << component->errors();
    } else {
        QObject *myObject = component->create();
    }
}
class PySide6.QtQml.QQmlComponent([parent=None])#

PySide6.QtQml.QQmlComponent(arg__1[, parent=None])

PySide6.QtQml.QQmlComponent(arg__1, fileName[, parent=None])

PySide6.QtQml.QQmlComponent(arg__1, fileName, mode[, parent=None])

PySide6.QtQml.QQmlComponent(arg__1, url[, parent=None])

PySide6.QtQml.QQmlComponent(arg__1, url, mode[, parent=None])

PySide6.QtQml.QQmlComponent(engine, uri, typeName[, parent=None])

PySide6.QtQml.QQmlComponent(engine, uri, typeName, mode[, parent=None])

Parameters:

Create a QQmlComponent with no data and give it the specified engine and parent. Set the data with setData() .

Create a QQmlComponent from the given fileName and give it the specified parent and engine.

See also

loadUrl()

Create a QQmlComponent from the given fileName and give it the specified parent and engine. If mode is Asynchronous , the component will be loaded and compiled asynchronously.

See also

loadUrl()

Create a QQmlComponent from the given url and give it the specified parent and engine.

Ensure that the URL provided is full and correct, in particular, use fromLocalFile() when loading a file from the local filesystem.

Relative paths will be resolved against baseUrl() , which is the current working directory unless specified.

See also

loadUrl()

Create a QQmlComponent from the given url and give it the specified parent and engine. If mode is Asynchronous , the component will be loaded and compiled asynchronously.

Ensure that the URL provided is full and correct, in particular, use fromLocalFile() when loading a file from the local filesystem.

Relative paths will be resolved against baseUrl() , which is the current working directory unless specified.

See also

loadUrl()

Create a QQmlComponent from the given uri and typeName and give it the specified parent and engine. If possible, the component will be loaded synchronously.

This is an overloaded function.

See also

loadFromModule()

Create a QQmlComponent from the given uri and typeName and give it the specified parent and engine. If mode is Asynchronous , the component will be loaded and compiled asynchronously.

This is an overloaded function.

See also

loadFromModule()

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property PᅟySide6.QtQml.QQmlComponent.progress: float#

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

Access functions:
property PᅟySide6.QtQml.QQmlComponent.status: Status#

The component’s current status .

Access functions:
property PᅟySide6.QtQml.QQmlComponent.url: PySide6.QtCore.QUrl#

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

Access functions:
PySide6.QtQml.QQmlComponent.CompilationMode#

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

Constant

Description

QQmlComponent.PreferSynchronous

Prefer loading/compiling the component immediately, blocking the thread. This is not always possible; for example, remote URLs will always load asynchronously.

QQmlComponent.Asynchronous

Load/compile the component in a background thread.

PySide6.QtQml.QQmlComponent.Status#

Specifies the loading status of the QQmlComponent .

Constant

Description

QQmlComponent.Null

This QQmlComponent has no data. Call loadUrl() or setData() to add QML content.

QQmlComponent.Ready

This QQmlComponent is ready and create() may be called.

QQmlComponent.Loading

This QQmlComponent is loading network data.

QQmlComponent.Error

An error has occurred. Call errors() to retrieve a list of errors .

PySide6.QtQml.QQmlComponent.beginCreate(arg__1)#
Parameters:

arg__1PySide6.QtQml.QQmlContext

Return type:

PySide6.QtCore.QObject

Create an object instance from this component, within the specified context. Returns None if creation failed.

Note

This method provides advanced control over component instance creation. In general, programmers should use create() to create object instances.

When QQmlComponent constructs an instance, it occurs in three steps:

  1. The object hierarchy is created, and constant values are assigned.

  2. Property bindings are evaluated for the first time.

  3. If applicable, componentComplete() is called on objects.

QQmlComponent::beginCreate() differs from create() in that it only performs step 1. completeCreate() must be called to complete steps 2 and 3.

This breaking point is sometimes useful when using attached properties to communicate information to an instantiated component, as it allows their initial values to be configured before property bindings take effect.

The ownership of the returned object instance is transferred to the caller.

See also

completeCreate() ObjectOwnership

PySide6.QtQml.QQmlComponent.completeCreate()#

This method provides advanced control over component instance creation. In general, programmers should use create() to create a component.

This function completes the component creation begun with beginCreate() and must be called afterwards.

See also

beginCreate()

PySide6.QtQml.QQmlComponent.create([context=None])#
Parameters:

contextPySide6.QtQml.QQmlContext

Return type:

PySide6.QtCore.QObject

Create an object instance from this component, within the specified context. Returns None if creation failed.

If context is None (the default), it will create the instance in the root context of the engine.

The ownership of the returned object instance is transferred to the caller.

If the object being created from this component is a visual item, it must have a visual parent, which can be set by calling setParentItem() . See Concepts - Visual Parent in Qt Quick for more details.

See also

ObjectOwnership

PySide6.QtQml.QQmlComponent.create(arg__1[, context=None[, forContext=None]])
Parameters:

Create an object instance from this component using the provided incubator. context specifies the context within which to create the object instance.

If context is None (by default), it will create the instance in the engine’s root context .

forContext specifies a context that this object creation depends upon. If the forContext is being created asynchronously, and the IncubationMode is AsynchronousIfNested , this object will also be created asynchronously. If forContext is None (by default), the context will be used for this decision.

The created object and its creation status are available via the incubator.

See also

QQmlIncubator

PySide6.QtQml.QQmlComponent.createObject([parent=None[, properties={}]])#
Parameters:
Return type:

PySide6.QtCore.QObject

PySide6.QtQml.QQmlComponent.createWithInitialProperties(initialProperties[, context=None])#
Parameters:
Return type:

PySide6.QtCore.QObject

Create an object instance of this component, within the specified context, and initialize its top-level properties with initialProperties.

If any of the initialProperties cannot be set, a warning is issued. If there are unset required properties, the object creation fails and returns nullptr, in which case isError() will return true.

See also

create

PySide6.QtQml.QQmlComponent.creationContext()#
Return type:

PySide6.QtQml.QQmlContext

Returns the QQmlContext the component was created in. This is only valid for components created directly from QML.

PySide6.QtQml.QQmlComponent.engine()#
Return type:

PySide6.QtQml.QQmlEngine

Returns the QQmlEngine of this component.

PySide6.QtQml.QQmlComponent.errorString()#
Return type:

str

PySide6.QtQml.QQmlComponent.errors()#
Return type:

Returns the list of errors that occurred during the last compile or create operation. An empty list is returned if isError() is not set.

PySide6.QtQml.QQmlComponent.isBound()#
Return type:

bool

Returns true if the component was created in a QML files that specifies pragma ComponentBehavior: Bound, otherwise returns false.

PySide6.QtQml.QQmlComponent.isError()#
Return type:

bool

Returns true if status() == Error .

PySide6.QtQml.QQmlComponent.isLoading()#
Return type:

bool

Returns true if status() == Loading .

PySide6.QtQml.QQmlComponent.isNull()#
Return type:

bool

Returns true if status() == Null .

PySide6.QtQml.QQmlComponent.isReady()#
Return type:

bool

Returns true if status() == Ready .

PySide6.QtQml.QQmlComponent.loadFromModule(uri, typeName[, mode=QQmlComponent.CompilationMode.PreferSynchronous])#
Parameters:
  • uriQAnyStringView

  • typeNameQAnyStringView

  • modeCompilationMode

Load the QQmlComponent for typeName in the module uri. If the type is implemented via a QML file, mode is used to load it. Types backed by C++ are always loaded synchronously.

QQmlEngine engine;
QQmlComponent component(&engine);
component.loadFromModule("QtQuick", "Item");
// once the component is ready
std::unique_ptr<QObject> item(component.create());
Q_ASSERT(item->metaObject() == &QQuickItem::staticMetaObject);

See also

loadUrl()

PySide6.QtQml.QQmlComponent.loadUrl(url)#
Parameters:

urlPySide6.QtCore.QUrl

Load the QQmlComponent from the provided url.

Ensure that the URL provided is full and correct, in particular, use fromLocalFile() when loading a file from the local filesystem.

Relative paths will be resolved against baseUrl() , which is the current working directory unless specified.

PySide6.QtQml.QQmlComponent.loadUrl(url, mode)
Parameters:

Load the QQmlComponent from the provided url. If mode is Asynchronous , the component will be loaded and compiled asynchronously.

Ensure that the URL provided is full and correct, in particular, use fromLocalFile() when loading a file from the local filesystem.

Relative paths will be resolved against baseUrl() , which is the current working directory unless specified.

PySide6.QtQml.QQmlComponent.progress()#
Return type:

float

Getter of property progress .

PySide6.QtQml.QQmlComponent.progressChanged(arg__1)#
Parameters:

arg__1 – float

Emitted whenever the component’s loading progress changes. progress will be the current progress between 0.0 (nothing loaded) and 1.0 (finished).

Notification signal of property progress .

PySide6.QtQml.QQmlComponent.setData(arg__1, baseUrl)#
Parameters:

Sets the QQmlComponent to use the given QML data. If url is provided, it is used to set the component name and to provide a base path for items resolved by this component.

PySide6.QtQml.QQmlComponent.setInitialProperties(component, properties)#
Parameters:

Set top-level properties of the component.

This method provides advanced control over component instance creation. In general, programmers should use createWithInitialProperties to create a component.

Use this method after beginCreate and before completeCreate has been called. If a provided property does not exist, a warning is issued.

PySide6.QtQml.QQmlComponent.status()#
Return type:

Status

Getter of property status .

PySide6.QtQml.QQmlComponent.statusChanged(arg__1)#
Parameters:

arg__1Status

Emitted whenever the component’s status changes. status will be the new status.

Notification signal of property status .

PySide6.QtQml.QQmlComponent.url()#
Return type:

PySide6.QtCore.QUrl

Getter of property url .