From 91ed4cdae1db4f051ac5dddd8b9197c96cf28ac3 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Wed, 10 Nov 2021 17:26:28 +0100 Subject: Make QQmlEngine take care of QQmlContext setup Teach QQmlEnginePrivate to create QQmlContextData for object creation purposes. This is necessary for both QQmlObjectCreator and qmltc's generated code Since tst_qmltc_manual used to test roughly the same code, replace the test-local implementation with the new one Task-number: QTBUG-84368 Change-Id: I58a1d4b259f42170d6dfb3809b7dd894bcbacbe1 Reviewed-by: Ulf Hermann --- tests/auto/qml/qmltc_manual/testclasses.h | 22 +++++++++++-- tests/auto/qml/qmltc_manual/tst_qmltc_manual.cpp | 39 ------------------------ 2 files changed, 20 insertions(+), 41 deletions(-) (limited to 'tests/auto/qml/qmltc_manual') diff --git a/tests/auto/qml/qmltc_manual/testclasses.h b/tests/auto/qml/qmltc_manual/testclasses.h index 2f1d8ed1ee..eecc9287ae 100644 --- a/tests/auto/qml/qmltc_manual/testclasses.h +++ b/tests/auto/qml/qmltc_manual/testclasses.h @@ -39,6 +39,7 @@ #include #include +#include // utility class that sets up QQmlContext for passed QObject. can be used as a // base class to ensure that qmlEngine(object) is valid during initializer list @@ -46,11 +47,28 @@ struct ContextRegistrator { ContextRegistrator(QQmlEngine *engine, QObject *This); + static QQmlRefPointer create(QQmlEngine *engine, const QUrl &url, - const QQmlRefPointer &parentContext, int index); + const QQmlRefPointer &parentContext, int index) + { + // test-specific wrapping that is document-root-agnostic. calls proper + // creation for index == 0 and falls back to returning parentContext + // otherwise. the qmltc generates exactly that but with an implicit + // index check + if (index == 0) { + auto priv = QQmlEnginePrivate::get(engine); + return priv->createInternalContext(priv->compilationUnitFromUrl(url), parentContext, 0, + true); + } + return parentContext; + } + static void set(QObject *This, const QQmlRefPointer &context, - QQmlContextData::QmlObjectKind kind); + QQmlContextData::QmlObjectKind kind) + { + QQmlEnginePrivate::setInternalContext(This, context, kind); + } }; class HelloWorld : public QObject, public ContextRegistrator diff --git a/tests/auto/qml/qmltc_manual/tst_qmltc_manual.cpp b/tests/auto/qml/qmltc_manual/tst_qmltc_manual.cpp index d4056c3628..5628b6e1d4 100644 --- a/tests/auto/qml/qmltc_manual/tst_qmltc_manual.cpp +++ b/tests/auto/qml/qmltc_manual/tst_qmltc_manual.cpp @@ -585,45 +585,6 @@ ContextRegistrator::ContextRegistrator(QQmlEngine *engine, QObject *This) Q_ASSERT(qmlEngine(This)); } -QQmlRefPointer -ContextRegistrator::create(QQmlEngine *engine, const QUrl &url, - const QQmlRefPointer &parentContext, int index) -{ - Q_ASSERT(index >= 0); - QQmlRefPointer context; - if (index == 0) { - // create context the same way it is done in QQmlObjectCreator::create() - context = QQmlContextData::createRefCounted(parentContext); - context->setInternal(true); - auto unit = QQmlEnginePrivate::get(engine)->compilationUnitFromUrl(url); - context->setImports(unit->typeNameCache); - context->initFromTypeCompilationUnit(unit, index); - } else { - // non-root objects adopt parent context and use that one instead of - // creating own - context = parentContext; - // assume context is initialized in the root object - } - return context; -} - -void ContextRegistrator::set(QObject *This, const QQmlRefPointer &context, - QQmlContextData::QmlObjectKind kind) -{ - Q_ASSERT(This); - QQmlData *ddata = QQmlData::get(This, /*create*/ true); - - // NB: copied from QQmlObjectCreator::createInstance() - // - // the if-statement logic is: if (static_cast(index) == 0 || - // ddata->rootObjectInCreation || isInlineComponent) then - // QQmlContextData::DocumentRoot - context->installContext(ddata, kind); - if (kind == QQmlContextData::DocumentRoot) - context->setContextObject(This); - Q_ASSERT(qmlEngine(This)); -} - HelloWorld::HelloWorld(QQmlEngine *e, QObject *parent) : QObject(parent), ContextRegistrator(e, this) { -- cgit v1.2.3