Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
40{
41 Q_DECLARE_PUBLIC(QQmlComponent)
42
43public:
45 : progress(0.), start(-1), engine(nullptr) {}
46
48
49 QObject *beginCreate(QQmlRefPointer<QQmlContextData>);
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,
62 const QQmlRefPointer<QQmlContextData> &context,
63 const QQmlRefPointer<QQmlContextData> &forContext);
64
65 QQmlRefPointer<QQmlTypeData> typeData;
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
81 QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit;
83
85 {
86 AnnotatedQmlError() = default;
87
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,
131 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
132 const QQmlRefPointer<QQmlContextData> &creationContext);
133
134 QList<AnnotatedQmlError> errors;
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
167 QObject *createWithProperties(QObject *parent, const QVariantMap &properties,
168 QQmlContext *context, CreateBehavior behavior = CreateDefault);
169
170 bool isBound() const { return compilationUnit && (compilationUnit->componentsAreBound()); }
171 LoadHelper::ResolveTypeResult prepareLoadFromModule(QAnyStringView uri,
173 void completeLoadFromModule(QAnyStringView uri, QAnyStringView typeName, QQmlType type,
176};
177
179{
180 if (m_creatorOrRequiredProperties.isT1())
181 delete m_creatorOrRequiredProperties.asT1();
182 else
183 delete m_creatorOrRequiredProperties.asT2();
184}
185
187{
188 errors = std::move(other.errors);
189 m_creatorOrRequiredProperties = std::exchange(other.m_creatorOrRequiredProperties, {});
190}
191
197 if (m_creatorOrRequiredProperties.isNull())
198 return nullptr;
199 else if (m_creatorOrRequiredProperties.isT1())
200 return m_creatorOrRequiredProperties.asT1()->requiredProperties();
201 else
202 return m_creatorOrRequiredProperties.asT2();
203}
204
206 const QObject *object, const QQmlPropertyData *propData, const RequiredPropertyInfo &info)
207{
208 Q_ASSERT(requiredProperties());
209 requiredProperties()->insert({object, propData}, info);
210}
211
213 auto properties = const_cast<ConstructionState *>(this)->requiredProperties();
214 return properties && !properties->isEmpty();
215}
216
218{
219 if (auto reqProps = requiredProperties())
220 reqProps->clear();
221}
222
223inline void QQmlComponentPrivate::ConstructionState::appendErrors(const QList<QQmlError> &qmlErrors)
224{
225 for (const QQmlError &e : qmlErrors)
226 errors.emplaceBack(e);
227}
228
231{
232 if (!hasCreator())
233 return;
234 auto creatorErrorCount = creator()->errors.size();
235 if (creatorErrorCount == 0)
236 return;
237 auto existingErrorCount = errors.size();
238 errors.resize(existingErrorCount + creatorErrorCount);
239 for (qsizetype i = 0; i < creatorErrorCount; ++i)
240 errors[existingErrorCount + i] = AnnotatedQmlError { std::move(creator()->errors[i]) };
241 creator()->errors.clear();
242}
243
245{
246 if (m_creatorOrRequiredProperties.isT1())
247 return m_creatorOrRequiredProperties.asT1();
248 return nullptr;
249}
250
252{
253 if (m_creatorOrRequiredProperties.isT1())
254 return m_creatorOrRequiredProperties.asT1();
255 return nullptr;
256}
257
259{
260 return creator() != nullptr;
261}
262
264{
265 if (m_creatorOrRequiredProperties.isT1()) {
266 delete m_creatorOrRequiredProperties.asT1();
267 m_creatorOrRequiredProperties = static_cast<QQmlObjectCreator *>(nullptr);
268 }
269}
270
271inline QQmlObjectCreator *QQmlComponentPrivate::ConstructionState::initCreator(QQmlRefPointer<QQmlContextData> parentContext, const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit, const QQmlRefPointer<QQmlContextData> &creationContext)
272{
273 if (m_creatorOrRequiredProperties.isT1())
274 delete m_creatorOrRequiredProperties.asT1();
275 else
276 delete m_creatorOrRequiredProperties.asT2();
277 m_creatorOrRequiredProperties = new QQmlObjectCreator(
278 std::move(parentContext), compilationUnit,
280 return m_creatorOrRequiredProperties.asT1();
281}
282
284{
285 return m_creatorOrRequiredProperties.flag();
286}
287
289{
290 m_creatorOrRequiredProperties.setFlagValue(isPending);
291}
292
300{
301 Q_ASSERT(m_creatorOrRequiredProperties.isT2() || m_creatorOrRequiredProperties.isNull());
302 if (m_creatorOrRequiredProperties.isNull())
303 m_creatorOrRequiredProperties = new RequiredProperties;
304}
305
307
308#endif // QQMLCOMPONENT_P_H
\inmodule QtCore
T * asT1() const
bool isT1() const
T2 * asT2() const
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:150
\inmodule QtCore
Definition qobject.h:103
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:129
\inmodule QtCore
Definition qurl.h:94
\inmodule QtCore
Definition qvariant.h:65
b clear()
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]
const char * typeName
GLsizei const GLfloat * v
[13]
GLenum mode
GLuint object
[3]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
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:75
static void initCreator(QQmlData::DeferredData *deferredData, const QQmlRefPointer< QQmlContextData > &contextData, QQmlComponentPrivate::ConstructionState *immediateState)
Definition qqmlbind.cpp:741
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
ptrdiff_t qsizetype
Definition qtypes.h:165
double qreal
Definition qtypes.h:187
static bool isTransient(const QWindow *w)
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
QItemEditorCreatorBase * creator
QHostInfo info
[0]
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)