Home

QQmlComponent

QQmlComponent is a class in the Qt QML framework that represents a QML component as loaded from a source and ready to be instantiated. It belongs to the QtQml module and is used to load, compile and create QML objects at runtime. A component can be created with a reference to a QQmlEngine or as a QObject, and it can load QML from a URL or from inline data. Source can be provided via setSource(QUrl) or setData(const QByteArray& data, bool isFromFile). The URL identifies the QML file, while the data string contains the QML code. The component maintains a status via the Status enum: Null, Loading, Ready, and Error. The errors() function returns a list of QQmlError objects that describe syntax or runtime issues encountered during loading or parsing.

Once the component is Ready, instances can be created by calling create(QQmlContext*) which returns a pointer

If the component references other JavaScript types or modules, the loader resolves them using the associated

to
the
newly
created
QObject.
The
created
object
becomes
part
of
the
given
QQmlContext;
the
component
can
be
used
to
instantiate
multiple
objects.
For
asynchronous
scenarios,
QQmlComponent
provides
beginCreate
and
completeCreate
to
support
staged
creation.
QQmlEngine.
The
component’s
lifecycle
is
tied
to
the
engine;
disposing
the
engine
invalidates
its
components.
Typical
usage
involves
creating
a
component,
loading
its
source,
checking
for
errors,
then
creating
objects
and
setting
properties
as
needed.