Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmlcomponent_p.h
Go to the documentation of this file.
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_P_H
5#define QQMLCOMPONENT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qqmlcomponent.h"
19
20#include "qqmlengine_p.h"
21#include "qqmlerror.h"
22#include <private/qqmlobjectcreator_p.h>
23#include <private/qqmltypedata_p.h>
24#include <private/qqmlguardedcontextdata_p.h>
25
26#include <QtCore/QString>
27#include <QtCore/QStringList>
28#include <QtCore/QList>
29#include <QtCore/qtclasshelpermacros.h>
30
31#include <private/qobject_p.h>
32
34
35class QQmlComponent;
36class QQmlEngine;
37
39class Q_QML_PRIVATE_EXPORT QQmlComponentPrivate : public QObjectPrivate, public QQmlTypeData::TypeDataCallback
40{
41 Q_DECLARE_PUBLIC(QQmlComponent)
42
43public:
45 : progress(0.), start(-1), engine(nullptr) {}
46
48
50 void completeCreate();
51 void initializeObjectWithInitialProperties(QV4::QmlContext *qmlContext, const QV4::Value &valuemap, QObject *toCreate, RequiredProperties *requiredProperties);
52 static void setInitialProperties(
54 const QV4::Value &v, RequiredProperties *requiredProperties, QObject *createdComponent,
56 static QQmlError unsetRequiredPropertyToQQmlError(const RequiredPropertyInfo &unsetRequiredProperty);
57
58 virtual void incubateObject(
59 QQmlIncubator *incubationTask,
63 const QQmlRefPointer<QQmlContextData> &forContext);
64
66 void typeDataReady(QQmlTypeData *) override;
67 void typeDataProgress(QQmlTypeData *, qreal) override;
68
69 void fromTypeData(const QQmlRefPointer<QQmlTypeData> &data);
70
73 std::unique_ptr<QString> inlineComponentName;
74
75 /* points to the sub-object in a QML file that should be instantiated
76 used create instances of QtQml's Component type and indirectly for inline components */
77 int start;
78
79 bool hadTopLevelRequiredProperties() const;
80 // TODO: merge compilation unit and type
83
85 {
86 AnnotatedQmlError() = default;
87
89 : error(std::move(error))
90 {
91 }
92
93
95 : error(std::move(error)), isTransient(transient)
96 {
97 }
99 bool isTransient = false; // tells if the error is temporary (e.g. unset required property)
100 };
101
103 ConstructionState() = default;
104 inline ~ConstructionState();
105 Q_DISABLE_COPY(ConstructionState)
106 inline ConstructionState(ConstructionState &&other) noexcept;
107
109 {
110 m_creatorOrRequiredProperties.swap(other.m_creatorOrRequiredProperties);
111 }
112
114
115 inline void ensureRequiredPropertyStorage();
116 inline RequiredProperties *requiredProperties();
117 inline void addPendingRequiredProperty(
118 const QObject *object, const QQmlPropertyData *propData,
120 inline bool hasUnsetRequiredProperties() const;
121 inline void clearRequiredProperties();
122
123 inline void appendErrors(const QList<QQmlError> &qmlErrors);
124 inline void appendCreatorErrors();
125
126 inline QQmlObjectCreator *creator();
127 inline const QQmlObjectCreator *creator() const;
128 inline void clear();
129 inline bool hasCreator() const;
130 inline QQmlObjectCreator *initCreator(QQmlRefPointer<QQmlContextData> parentContext,
132 const QQmlRefPointer<QQmlContextData> &creationContext);
133
135 inline bool isCompletePending() const;
136 inline void setCompletePending(bool isPending);
137
138 private:
139 QBiPointer<QQmlObjectCreator, RequiredProperties> m_creatorOrRequiredProperties;
140 };
142
143 using DeferredState = std::vector<ConstructionState>;
144 static void beginDeferred(QQmlEnginePrivate *enginePriv, QObject *object, DeferredState* deferredState);
145 static void completeDeferred(QQmlEnginePrivate *enginePriv, DeferredState *deferredState);
146
147 static void complete(QQmlEnginePrivate *enginePriv, ConstructionState *state);
148 static QQmlProperty removePropertyFromRequired(QObject *createdComponent, const QString &name, RequiredProperties *requiredProperties,
149 QQmlEngine *engine, bool *wasInRequiredProperties = nullptr);
150
153
154 void clear();
155
157 return static_cast<QQmlComponentPrivate *>(QObjectPrivate::get(c));
158 }
159
160 QObject *doBeginCreate(QQmlComponent *q, QQmlContext *context);
161 bool setInitialProperty(QObject *component, const QString &name, const QVariant& value);
162
166 };
167 QObject *createWithProperties(QObject *parent, const QVariantMap &properties,
168 QQmlContext *context, CreateBehavior behavior = CreateDefault);
169
170 bool isBound() const {
171 return compilationUnit
172 && (compilationUnit->unitData()->flags & QV4::CompiledData::Unit::ComponentsBound);
173 }
174};
175
177{
178 if (m_creatorOrRequiredProperties.isT1())
179 delete m_creatorOrRequiredProperties.asT1();
180 else
181 delete m_creatorOrRequiredProperties.asT2();
182}
183
185{
186 errors = std::move(other.errors);
187 m_creatorOrRequiredProperties = std::exchange(other.m_creatorOrRequiredProperties, {});
188}
189
195 if (m_creatorOrRequiredProperties.isNull())
196 return nullptr;
197 else if (m_creatorOrRequiredProperties.isT1())
198 return m_creatorOrRequiredProperties.asT1()->requiredProperties();
199 else
200 return m_creatorOrRequiredProperties.asT2();
201}
202
204 const QObject *object, const QQmlPropertyData *propData, const RequiredPropertyInfo &info)
205{
206 Q_ASSERT(requiredProperties());
207 requiredProperties()->insert({object, propData}, info);
208}
209
211 auto properties = const_cast<ConstructionState *>(this)->requiredProperties();
212 return properties && !properties->isEmpty();
213}
214
216{
217 if (auto reqProps = requiredProperties())
218 reqProps->clear();
219}
220
222{
223 for (const QQmlError &e : qmlErrors)
224 errors.emplaceBack(e);
225}
226
229{
230 if (!hasCreator())
231 return;
232 auto creatorErrorCount = creator()->errors.size();
233 if (creatorErrorCount == 0)
234 return;
235 auto existingErrorCount = errors.size();
236 errors.resize(existingErrorCount + creatorErrorCount);
237 for (qsizetype i = 0; i < creatorErrorCount; ++i)
238 errors[existingErrorCount + i] = AnnotatedQmlError { std::move(creator()->errors[i]) };
239 creator()->errors.clear();
240}
241
243{
244 if (m_creatorOrRequiredProperties.isT1())
245 return m_creatorOrRequiredProperties.asT1();
246 return nullptr;
247}
248
250{
251 if (m_creatorOrRequiredProperties.isT1())
252 return m_creatorOrRequiredProperties.asT1();
253 return nullptr;
254}
255
257{
258 return creator() != nullptr;
259}
260
262{
263 if (m_creatorOrRequiredProperties.isT1()) {
264 delete m_creatorOrRequiredProperties.asT1();
265 m_creatorOrRequiredProperties = static_cast<QQmlObjectCreator *>(nullptr);
266 }
267}
268
270{
271 if (m_creatorOrRequiredProperties.isT1())
272 delete m_creatorOrRequiredProperties.asT1();
273 else
274 delete m_creatorOrRequiredProperties.asT2();
275 m_creatorOrRequiredProperties = new QQmlObjectCreator(
276 std::move(parentContext), compilationUnit,
278 return m_creatorOrRequiredProperties.asT1();
279}
280
282{
283 return m_creatorOrRequiredProperties.flag();
284}
285
287{
288 m_creatorOrRequiredProperties.setFlagValue(isPending);
289}
290
298{
299 Q_ASSERT(m_creatorOrRequiredProperties.isT2() || m_creatorOrRequiredProperties.isNull());
300 if (m_creatorOrRequiredProperties.isNull())
301 m_creatorOrRequiredProperties = new RequiredProperties;
302}
303
305
306#endif // QQMLCOMPONENT_P_H
T * asT1() const
bool isT1() const
T2 * asT2() const
Definition qlist.h:74
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:153
\inmodule QtCore
Definition qobject.h:90
ConstructionState state
QQmlRefPointer< QV4::ExecutableCompilationUnit > compilationUnit
std::vector< ConstructionState > DeferredState
std::unique_ptr< QString > inlineComponentName
static QQmlComponentPrivate * get(QQmlComponent *c)
QQmlGuardedContextData creationContext
QQmlRefPointer< QQmlTypeData > typeData
The QQmlComponent class encapsulates a QML component definition.
CompilationMode
Specifies whether the QQmlComponent should load the component immediately, or asynchonously.
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
The QQmlError class encapsulates a QML error.
Definition qqmlerror.h:18
The QQmlIncubator class allows QML objects to be created asynchronously.
The QQmlProperty class abstracts accessing properties on objects created from QML.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qurl.h:94
\inmodule QtCore
Definition qvariant.h:64
b clear()
double e
else opt state
[0]
Combined button and popup list for selecting options.
static void * context
static const QCssKnownValue properties[NumProperties - 1]
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLsizei const GLfloat * v
[13]
GLenum mode
GLuint object
[3]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint start
GLuint name
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
static qreal component(const QPointF &point, unsigned int i)
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:71
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
ptrdiff_t qsizetype
Definition qtypes.h:70
double qreal
Definition qtypes.h:92
static bool isTransient(const QWindow *w)
QFileInfo info(fileName)
[8]
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
QItemEditorCreatorBase * creator
QJSEngine engine
[0]
AnnotatedQmlError(QQmlError error, bool transient)
void appendErrors(const QList< QQmlError > &qmlErrors)
void addPendingRequiredProperty(const QObject *object, const QQmlPropertyData *propData, const RequiredPropertyInfo &info)
void swap(ConstructionState &other)
QQmlObjectCreator * initCreator(QQmlRefPointer< QQmlContextData > parentContext, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &compilationUnit, const QQmlRefPointer< QQmlContextData > &creationContext)
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QQmlComponentPrivate::ConstructionState)
virtual void typeDataProgress(QQmlTypeData *, qreal)
virtual void typeDataReady(QQmlTypeData *)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent