41 Q_DECLARE_PUBLIC(QQmlComponent)
44 QQmlComponentPrivate()
45 : progress(0.), start(-1), engine(
nullptr) {}
47 void loadUrl(
const QUrl &newUrl, QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous);
49 QQmlType loadedType()
const {
return loadHelper ? loadHelper->type() : QQmlType(); }
51 QObject *beginCreate(QQmlRefPointer<QQmlContextData>);
52 void completeCreate();
53 void initializeObjectWithInitialProperties(QV4::QmlContext *qmlContext,
const QV4::Value &valuemap, QObject *toCreate, RequiredProperties *requiredProperties);
54 static void setInitialProperties(
55 QV4::ExecutionEngine *engine, QV4::QmlContext *qmlContext,
const QV4::Value &o,
56 const QV4::Value &v, RequiredProperties *requiredProperties, QObject *createdComponent,
57 QQmlObjectCreator *creator);
58 static QQmlError unsetRequiredPropertyToQQmlError(
const RequiredPropertyInfo &unsetRequiredProperty);
60 virtual void incubateObject(
61 QQmlIncubator *incubationTask,
62 QQmlComponent *component,
64 const QQmlRefPointer<QQmlContextData> &context,
65 const QQmlRefPointer<QQmlContextData> &forContext);
67 QQmlRefPointer<QQmlTypeData> typeData;
68 QQmlRefPointer<LoadHelper> loadHelper;
70 void typeDataReady(QQmlTypeData *) override;
71 void typeDataProgress(QQmlTypeData *, qreal) override;
73 void fromTypeData(
const QQmlRefPointer<QQmlTypeData> &data);
77 std::unique_ptr<QString> inlineComponentName;
80
83 bool hadTopLevelRequiredProperties()
const;
84 QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit;
86 struct AnnotatedQmlError
88 AnnotatedQmlError() =
default;
90 AnnotatedQmlError(QQmlError error)
91 : error(std::move(error))
96 AnnotatedQmlError(QQmlError error,
bool transient)
97 : error(std::move(error)), isTransient(transient)
101 bool isTransient =
false;
104 struct ConstructionState {
105 ConstructionState() =
default;
106 inline ~ConstructionState();
107 Q_DISABLE_COPY(ConstructionState)
108 inline ConstructionState(ConstructionState &&other)
noexcept;
110 void swap(ConstructionState &other)
112 m_creatorOrRequiredProperties.swap(other.m_creatorOrRequiredProperties);
115 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QQmlComponentPrivate::ConstructionState);
117 inline void ensureRequiredPropertyStorage(QObject *target);
118 inline RequiredProperties *requiredProperties();
119 inline void addPendingRequiredProperty(
120 const QObject *object,
const QQmlPropertyData *propData,
121 const RequiredPropertyInfo &info);
122 inline bool hasUnsetRequiredProperties()
const;
123 inline void clearRequiredProperties();
125 inline void appendErrors(
const QList<QQmlError> &qmlErrors);
126 inline void appendCreatorErrors();
128 inline QQmlObjectCreator *creator();
129 inline const QQmlObjectCreator *creator()
const;
131 inline bool hasCreator()
const;
132 inline QQmlObjectCreator *initCreator(
133 const QQmlRefPointer<QQmlContextData> &parentContext,
134 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
135 const QQmlRefPointer<QQmlContextData> &creationContext);
137 QList<AnnotatedQmlError> errors;
138 inline bool isCompletePending()
const;
139 inline void setCompletePending(
bool isPending);
141 QObject *target()
const
143 if (m_creatorOrRequiredProperties.isNull())
146 if (m_creatorOrRequiredProperties.isT1()) {
147 const auto &objects = m_creatorOrRequiredProperties.asT1()->allCreatedObjects();
148 return objects.isEmpty() ?
nullptr : objects.at(0);
151 Q_ASSERT(m_creatorOrRequiredProperties.isT2());
152 return m_creatorOrRequiredProperties.asT2()->target;
156 QBiPointer<QQmlObjectCreator, RequiredPropertiesAndTarget> m_creatorOrRequiredProperties;
158 ConstructionState state;
160 using DeferredState = std::vector<ConstructionState>;
161 static void beginDeferred(QQmlEnginePrivate *enginePriv, QObject *object, DeferredState* deferredState);
162 static void completeDeferred(QQmlEnginePrivate *enginePriv, DeferredState *deferredState);
164 static void complete(QQmlEnginePrivate *enginePriv, ConstructionState *state);
165 static QQmlProperty removePropertyFromRequired(QObject *createdComponent,
const QString &name, RequiredProperties *requiredProperties,
166 QQmlEngine *engine,
bool *wasInRequiredProperties =
nullptr);
169 QQmlGuardedContextData creationContext;
173 static QQmlComponentPrivate *get(QQmlComponent *c) {
174 return static_cast<QQmlComponentPrivate *>(QObjectPrivate::get(c));
177 QObject *doBeginCreate(QQmlComponent *q, QQmlContext *context);
178 bool setInitialProperty(QObject *component,
const QString &name,
const QVariant& value);
180 enum CreateBehavior {
182 CreateWarnAboutRequiredProperties,
184 QObject *createWithProperties(QObject *parent,
const QVariantMap &properties,
185 QQmlContext *context, CreateBehavior behavior = CreateDefault,
186 bool createFromQml =
false);
188 bool isBound()
const {
return compilationUnit && (compilationUnit->componentsAreBound()); }
189 void prepareLoadFromModule(
190 QAnyStringView uri, QAnyStringView typeName, QQmlTypeLoader::Mode mode);
191 void completeLoadFromModule(
192 QAnyStringView uri, QAnyStringView typeName);
247inline void QQmlComponentPrivate::ConstructionState::appendCreatorErrors()
251 auto creatorErrorCount = creator()->errors.size();
252 if (creatorErrorCount == 0)
254 auto existingErrorCount = errors.size();
255 errors.resize(existingErrorCount + creatorErrorCount);
256 for (qsizetype i = 0; i < creatorErrorCount; ++i)
257 errors[existingErrorCount + i] = AnnotatedQmlError { std::move(creator()->errors[i]) };
258 creator()->errors.clear();
288inline QQmlObjectCreator *QQmlComponentPrivate::ConstructionState::initCreator(
289 const QQmlRefPointer<QQmlContextData> &parentContext,
290 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
291 const QQmlRefPointer<QQmlContextData> &creationContext)
293 if (m_creatorOrRequiredProperties.isT1())
294 delete m_creatorOrRequiredProperties.asT1();
296 delete m_creatorOrRequiredProperties.asT2();
297 m_creatorOrRequiredProperties =
new QQmlObjectCreator(
298 parentContext, compilationUnit, creationContext);
299 return m_creatorOrRequiredProperties.asT1();