QQmlComponent¶
The
QQmlComponentclass encapsulates a QML component definition. More…

Synopsis¶
Functions¶
def
create(arg__1[, context=None[, forContext=None]])def
createWithInitialProperties(initialProperties[, context=None])def
creationContext()def
engine()def
errorString()def
errors()def
isError()def
isLoading()def
isNull()def
isReady()def
progress()def
setInitialProperties(component, properties)def
status()def
url()
Virtual functions¶
def
beginCreate(arg__1)def
completeCreate()def
create([context=None])
Slots¶
Signals¶
def
progressChanged(arg__1)def
statusChanged(arg__1)
Detailed Description¶
Components are reusable, encapsulated QML types with well-defined interfaces.
A
QQmlComponentinstance can be created from a QML file. For example, if there is amain.qmlfile 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 = 200To create instances of a component in code where a
QQmlEngineinstance is not available, you can useqmlContext()orqmlEngine(). For example, in the scenario below, child items are being created within aQQuickItemsubclass: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
nullwhen called inside the constructor of aQObjectsubclass, as the instance will not yet have a context nor engine.
Network Components¶
If the URL passed to
QQmlComponentis a network resource, or if the QML document references a network resource, theQQmlComponenthas to fetch the network data before it is able to create objects. In this case, theQQmlComponentwill have aLoadingstatus. An application will have to wait until the component isReadybefore callingcreate().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 thestatusChanged()signal and otherwise calls thecontinueLoading()method directly. Note thatisLoading()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(); } }
- class PySide2.QtQml.QQmlComponent([parent=None])¶
PySide2.QtQml.QQmlComponent(arg__1[, parent=None])
PySide2.QtQml.QQmlComponent(arg__1, fileName[, parent=None])
PySide2.QtQml.QQmlComponent(arg__1, fileName, mode[, parent=None])
PySide2.QtQml.QQmlComponent(arg__1, url[, parent=None])
PySide2.QtQml.QQmlComponent(arg__1, url, mode[, parent=None])
- param parent:
- param url:
- param arg__1:
- param fileName:
str
- param mode:
Create a
QQmlComponentwith no data and give it the specifiedengineandparent. Set the data withsetData().
- PySide2.QtQml.QQmlComponent.CompilationMode¶
Specifies whether the
QQmlComponentshould 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.
- PySide2.QtQml.QQmlComponent.Status¶
Specifies the loading status of the
QQmlComponent.Constant
Description
QQmlComponent.Null
This
QQmlComponenthas no data. CallloadUrl()orsetData()to add QML content.QQmlComponent.Ready
This
QQmlComponentis ready andcreate()may be called.QQmlComponent.Loading
This
QQmlComponentis loading network data.QQmlComponent.Error
An error has occurred. Call
errors()to retrieve a list oferrors.
- PySide2.QtQml.QQmlComponent.beginCreate(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtQml.QQmlContext- Return type:
This method provides advanced control over component instance creation. In general, programmers should use
create()to create object instances.Create an object instance from this component. Returns
Noneif creation failed.publicContextspecifies the context within which to create the object instance.When
QQmlComponentconstructs an instance, it occurs in three steps:The object hierarchy is created, and constant values are assigned.
Property bindings are evaluated for the first time.
If applicable,
componentComplete()is called on objects.
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
- PySide2.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
- PySide2.QtQml.QQmlComponent.create([context=None])¶
- Parameters:
context –
PySide2.QtQml.QQmlContext- Return type:
Create an object instance from this component. Returns
Noneif creation failed.contextspecifies the context within which to create the object instance.If
contextisNone(the default), it will create the instance in theroot contextof 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
- PySide2.QtQml.QQmlComponent.create(arg__1[, context=None[, forContext=None]])
- Parameters:
arg__1 –
PySide2.QtQml.QQmlIncubatorcontext –
PySide2.QtQml.QQmlContextforContext –
PySide2.QtQml.QQmlContext
Create an object instance from this component using the provided
incubator.contextspecifies the context within which to create the object instance.If
contextis 0 (the default), it will create the instance in the engine’sroot context.forContextspecifies a context that this object creation depends upon. If theforContextis being created asynchronously, and theIncubationModeisAsynchronousIfNested, this object will also be created asynchronously. IfforContextis 0 (the default), thecontextwill be used for this decision.The created object and its creation status are available via the
incubator.See also
- PySide2.QtQml.QQmlComponent.createWithInitialProperties(initialProperties[, context=None])¶
- Parameters:
initialProperties –
context –
PySide2.QtQml.QQmlContext
- Return type:
Create an object instance of this component, and initialize its toplevel properties with
initialProperties.contextspecifies the context where the object instance is to be created.If any of the
initialPropertiescannot be set,isError()will returntrue, and theerrors()function can be used to get detailed information about the error(s).See also
- PySide2.QtQml.QQmlComponent.creationContext()¶
- Return type:
Returns the
QQmlContextthe component was created in. This is only valid for components created directly from QML.
- PySide2.QtQml.QQmlComponent.engine()¶
- Return type:
Returns the
QQmlEngineof this component.
- PySide2.QtQml.QQmlComponent.errorString()¶
- Return type:
str
errorString is only meant as a way to get the errors in script
- PySide2.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.
- PySide2.QtQml.QQmlComponent.loadUrl(url)¶
- Parameters:
url –
PySide2.QtCore.QUrl
Load the
QQmlComponentfrom the providedurl.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.
- PySide2.QtQml.QQmlComponent.loadUrl(url, mode)
- Parameters:
url –
PySide2.QtCore.QUrlmode –
CompilationMode
Load the
QQmlComponentfrom the providedurl. IfmodeisAsynchronous, 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.
- PySide2.QtQml.QQmlComponent.progress()¶
- Return type:
float
The progress of loading the component, from 0.0 (nothing loaded) to 1.0 (finished).
- PySide2.QtQml.QQmlComponent.progressChanged(arg__1)¶
- Parameters:
arg__1 – float
- PySide2.QtQml.QQmlComponent.setData(arg__1, baseUrl)¶
- Parameters:
arg__1 –
PySide2.QtCore.QByteArraybaseUrl –
PySide2.QtCore.QUrl
Sets the
QQmlComponentto use the given QMLdata. Ifurlis provided, it is used to set the component name and to provide a base path for items resolved by this component.
- PySide2.QtQml.QQmlComponent.setInitialProperties(component, properties)¶
- Parameters:
component –
PySide2.QtCore.QObjectproperties –
Set toplevel
propertiesof thecomponent.This method provides advanced control over component instance creation. In general, programmers should use
createWithInitialPropertiesto create a component.Use this method after
beginCreateand beforecompleteCreatehas been called. If a provided property does not exist, a warning is issued.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.