QQmlComponent Class▲
-
Header: QQmlComponent
-
Since: Qt 5.0
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
-
qmake: QT += qml
-
Inherited By:
-
Instantiated By: qml-qtqml-component.xml
-
Inherits: https://
Detailed Description▲
Components are reusable, encapsulated QML types with well-defined interfaces.
A https:// 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 https://(), 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 https:// instance is not available, you can use https://() or https://(). 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 https:// subclass, as the instance will not yet have a context nor engine.
Network Components▲
If the URL passed to https:// is a network resource, or if the QML document references a network resource, the https:// has to fetch the network data before it is able to create objects. In this case, the https:// will have a https:// https://. An application will have to wait until the component is https:// before calling https://().
The following example shows how to load a QML file from a network resource. After creating the https://, it tests whether the component is loading. If it is, it connects to the https://() signal and otherwise calls the continueLoading() method directly. Note that https://() 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();
}
}Member Type Documentation▲
enum QQmlComponent::CompilationMode▲
Specifies whether the https:// should load the component immediately, or asynchonously.
|
Constant |
Value |
Description |
|---|---|---|
|
https://::PreferSynchronous |
0 |
Prefer loading/compiling the component immediately, blocking the thread. This is not always possible; for example, remote URLs will always load asynchronously. |
|
https://::Asynchronous |
1 |
Load/compile the component in a background thread. |
enum QQmlComponent::Status▲
Specifies the loading status of the https://.
|
Constant |
Value |
Description |
|---|---|---|
|
https://::Null |
0 |
This https:// has no data. Call https://() or https://() to add QML content. |
|
https://::Ready |
1 |
This https:// is ready and https://() may be called. |
|
https://::Loading |
2 |
This https:// is loading network data. |
|
https://::Error |
3 |
An error has occurred. Call https://() to retrieve a list of https://. |
Property Documentation▲
[read-only] progress : const qreal▲
The progress of loading the component, from 0.0 (nothing loaded) to 1.0 (finished).
Access functions:
-
progress() const
Notifier signal:
-
void https://( progress)
[read-only] status : const Status▲
The component's current https://.
Access functions:
-
status() const
Notifier signal:
-
void https://( status)
[read-only] url : const QUrl▲
The component URL. This is the URL passed to either the constructor, or the https://(), or https://() methods.
Access functions:
-
url() const
Member Function Documentation▲
QQmlComponent::QQmlComponent(QQmlEngine *engine, QObject *parent = nullptr)▲
Create a https:// with no data and give it the specified engine and parent. Set the data with https://().
QQmlComponent::QQmlComponent(QQmlEngine *engine, const QString &fileName, QObject *parent = nullptr)▲
Create a https:// from the given fileName and give it the specified parent and engine.
See Also▲
See also https://()
QQmlComponent::QQmlComponent(QQmlEngine *engine, const QString &fileName, QQmlComponent::CompilationMode mode, QObject *parent = nullptr)▲
Create a https:// from the given fileName and give it the specified parent and engine. If mode is https://, the component will be loaded and compiled asynchronously.
See Also▲
See also https://()
QQmlComponent::QQmlComponent(QQmlEngine *engine, const QUrl &url, QObject *parent = nullptr)▲
Create a https:// from the given url and give it the specified parent and engine.
Ensure that the URL provided is full and correct, in particular, use https://() when loading a file from the local filesystem.
Relative paths will be resolved against https://(), which is the current working directory unless specified.
See Also▲
See also https://()
QQmlComponent::QQmlComponent(QQmlEngine *engine, const QUrl &url, QQmlComponent::CompilationMode mode, QObject *parent = nullptr)▲
Create a https:// from the given url and give it the specified parent and engine. If mode is https://, the component will be loaded and compiled asynchronously.
Ensure that the URL provided is full and correct, in particular, use https://() when loading a file from the local filesystem.
Relative paths will be resolved against https://(), which is the current working directory unless specified.
See Also▲
See also https://()
[override virtual] QQmlComponent::~QQmlComponent()▲
Destruct the https://.
[virtual] QObject *QQmlComponent::beginCreate(QQmlContext *context)▲
Create an object instance from this component, within the specified context. Returns nullptr if creation failed.
This method provides advanced control over component instance creation. In general, programmers should use https://() to create object instances.
When https:// constructs 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, https://() is called on objects.
QQmlComponent::beginCreate() differs from https://() in that it only performs step 1. https://() 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▲
See also https://(), https://
[virtual] void QQmlComponent::completeCreate()▲
This method provides advanced control over component instance creation. In general, programmers should use https://() to create a component.
This function completes the component creation begun with https://() and must be called afterwards.
See Also▲
See also https://()
[virtual] QObject *QQmlComponent::create(QQmlContext *context = nullptr)▲
Create an object instance from this component, within the specified context. Returns nullptr if creation failed.
If context is nullptr (the default), it will create the instance in the https:// 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 QQuickItem::setParentItem(). See Concepts - Visual Parent in Qt Quick for more details.
See Also▲
See also https://
void QQmlComponent::create(QQmlIncubator &incubator, QQmlContext *context = nullptr, QQmlContext *forContext = nullptr)▲
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 nullptr (by default), it will create the instance in the engine's https://.
forContext specifies a context that this object creation depends upon. If the forContext is being created asynchronously, and the https:// is https://, this object will also be created asynchronously. If forContext is nullptr (by default), the context will be used for this decision.
The created object and its creation status are available via the incubator.
See Also▲
See also https://
[since 5.14] QObject *QQmlComponent::createWithInitialProperties(const QVariantMap &initialProperties, QQmlContext *context = nullptr)▲
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, https://() will return true, and the https://() function can be used to get detailed information about the error(s).
This function was introduced in Qt 5.14.
See Also▲
See also https://
QQmlContext *QQmlComponent::creationContext() const▲
Returns the https:// the component was created in. This is only valid for components created directly from QML.
[since 5.12] QQmlEngine *QQmlComponent::engine() const▲
Returns the https:// of this component.
This function was introduced in Qt 5.12.
QList<QQmlError> QQmlComponent::errors() const▲
Returns the list of errors that occurred during the last compile or create operation. An empty list is returned if https://() is not set.
bool QQmlComponent::isError() const▲
Returns true if https://() == https://.
bool QQmlComponent::isLoading() const▲
Returns true if https://() == https://.
bool QQmlComponent::isNull() const▲
Returns true if https://() == https://.
bool QQmlComponent::isReady() const▲
Returns true if https://() == https://.
void QQmlComponent::loadUrl(const QUrl &url)▲
Load the https:// from the provided url.
Ensure that the URL provided is full and correct, in particular, use https://() when loading a file from the local filesystem.
Relative paths will be resolved against https://(), which is the current working directory unless specified.
void QQmlComponent::loadUrl(const QUrl &url, QQmlComponent::CompilationMode mode)▲
Load the https:// from the provided url. If mode is https://, the component will be loaded and compiled asynchronously.
Ensure that the URL provided is full and correct, in particular, use https://() when loading a file from the local filesystem.
Relative paths will be resolved against https://(), which is the current working directory unless specified.
void QQmlComponent::progressChanged(qreal progress)▲
Emitted whenever the component's loading progress changes. progress will be the current progress between 0.0 (nothing loaded) and 1.0 (finished).
Notifier signal for property https://.
void QQmlComponent::setData(const QByteArray &data, const QUrl &url)▲
Sets the https:// 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.
[since 5.14] void QQmlComponent::setInitialProperties(QObject *component, const QVariantMap &properties)▲
Set top-level properties of the component.
This method provides advanced control over component instance creation. In general, programmers should use https:// to create a component.
Use this method after https:// and before https:// has been called. If a provided property does not exist, a warning is issued.
This function was introduced in Qt 5.14.
void QQmlComponent::statusChanged(QQmlComponent::Status status)▲
Emitted whenever the component's status changes. status will be the new status.
Notifier signal for property https://.


