1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLCOMPONENT_H
5#define QQMLCOMPONENT_H
6
7#include <QtCore/qvariant.h>
8#include <QtCore/qmap.h>
9
10#include <QtQml/qqml.h>
11#include <QtQml/qqmlerror.h>
12
13#include <QtCore/qobject.h>
14#include <QtCore/qstring.h>
15#include <QtQml/qjsvalue.h>
16
17QT_BEGIN_NAMESPACE
18
19
20class QByteArray;
21class QQmlEngine;
22class QQmlComponent;
23class QQmlIncubator;
24class QQmlV4Function;
25class QQmlComponentPrivate;
26class QQmlComponentAttached;
27
28namespace QV4 {
29class ExecutableCompilationUnit;
30}
31
32class Q_QML_EXPORT QQmlComponent : public QObject
33{
34 Q_OBJECT
35 Q_DECLARE_PRIVATE(QQmlComponent)
36
37 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
38 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
39 Q_PROPERTY(QUrl url READ url CONSTANT)
40 QML_NAMED_ELEMENT(Component)
41 QML_ADDED_IN_VERSION(2, 0)
42 QML_ATTACHED(QQmlComponentAttached)
43 Q_CLASSINFO("QML.OmitFromQmlTypes", "true")
44
45public:
46 enum CompilationMode { PreferSynchronous, Asynchronous };
47 Q_ENUM(CompilationMode)
48
49 QQmlComponent(QObject *parent = nullptr);
50 QQmlComponent(QQmlEngine *, QObject *parent = nullptr);
51 QQmlComponent(QQmlEngine *, const QString &fileName, QObject *parent = nullptr);
52 QQmlComponent(QQmlEngine *, const QString &fileName, CompilationMode mode, QObject *parent = nullptr);
53 QQmlComponent(QQmlEngine *, const QUrl &url, QObject *parent = nullptr);
54 QQmlComponent(QQmlEngine *, const QUrl &url, CompilationMode mode, QObject *parent = nullptr);
55
56 explicit QQmlComponent(QQmlEngine *engine, QAnyStringView uri, QAnyStringView typeName, QObject *parent = nullptr);
57 explicit QQmlComponent(QQmlEngine *engine, QAnyStringView uri, QAnyStringView typeName, CompilationMode mode, QObject *parent = nullptr);
58
59 ~QQmlComponent() override;
60
61 enum Status { Null, Ready, Loading, Error };
62 Q_ENUM(Status)
63 Status status() const;
64
65 bool isNull() const;
66 bool isReady() const;
67 bool isError() const;
68 bool isLoading() const;
69
70 bool isBound() const;
71
72 QList<QQmlError> errors() const;
73 Q_INVOKABLE QString errorString() const;
74
75 qreal progress() const;
76
77 QUrl url() const;
78
79 virtual QObject *create(QQmlContext *context = nullptr);
80 QObject *createWithInitialProperties(const QVariantMap& initialProperties, QQmlContext *context = nullptr);
81 void setInitialProperties(QObject *component, const QVariantMap &properties);
82 virtual QObject *beginCreate(QQmlContext *);
83 virtual void completeCreate();
84
85 void create(QQmlIncubator &, QQmlContext *context = nullptr,
86 QQmlContext *forContext = nullptr);
87
88 QQmlContext *creationContext() const;
89 QQmlEngine *engine() const;
90
91 static QQmlComponentAttached *qmlAttachedProperties(QObject *);
92
93public Q_SLOTS:
94 void loadUrl(const QUrl &url);
95 void loadUrl(const QUrl &url, CompilationMode mode);
96 void loadFromModule(QAnyStringView uri, QAnyStringView typeName,
97 QQmlComponent::CompilationMode mode = PreferSynchronous);
98 void setData(const QByteArray &, const QUrl &baseUrl);
99
100Q_SIGNALS:
101 void statusChanged(QQmlComponent::Status);
102 void progressChanged(qreal);
103
104protected:
105 QQmlComponent(QQmlComponentPrivate &dd, QObject* parent);
106
107#if QT_DEPRECATED_SINCE(6, 3)
108 QT_DEPRECATED_X("Use the overload with proper arguments")
109 Q_INVOKABLE void createObject(QQmlV4Function *);
110#endif
111
112 Q_INVOKABLE QObject *createObject(
113 QObject *parent = nullptr, const QVariantMap &properties = {});
114 Q_INVOKABLE void incubateObject(QQmlV4Function *);
115
116private:
117 QQmlComponent(QQmlEngine *, QV4::ExecutableCompilationUnit *compilationUnit, int,
118 QObject *parent);
119
120 Q_DISABLE_COPY(QQmlComponent)
121 friend class QQmlTypeData;
122 friend class QQmlObjectCreator;
123};
124
125
126// Don't do this at home.
127namespace QQmlPrivate {
128
129// Generally you cannot use QQmlComponentAttached as attached properties object in derived classes.
130// It is private.
131template<class T>
132struct OverridableAttachedType<T, QQmlComponentAttached>
133{
134 using Type = void;
135};
136
137// QQmlComponent itself is allowed to use QQmlComponentAttached, though.
138template<>
139struct OverridableAttachedType<QQmlComponent, QQmlComponentAttached>
140{
141 using Type = QQmlComponentAttached;
142};
143
144} // namespace QQmlPrivate
145
146
147QT_END_NAMESPACE
148QML_DECLARE_TYPE(QQmlComponent)
149
150#endif // QQMLCOMPONENT_H
151

source code of qtdeclarative/src/qml/qml/qqmlcomponent.h