diff options
author | Andrei Golubev <andrei.golubev@qt.io> | 2021-11-10 17:26:28 +0100 |
---|---|---|
committer | Andrei Golubev <andrei.golubev@qt.io> | 2021-11-12 18:57:43 +0100 |
commit | 91ed4cdae1db4f051ac5dddd8b9197c96cf28ac3 (patch) | |
tree | 409462d6729c19fcff6d7c363b82d0be3cc865e3 /tests/auto/qml/qmltc_manual | |
parent | e0db57f9d07f47e1fefdd0c6f507e32308e4041c (diff) |
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 <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/qml/qmltc_manual')
-rw-r--r-- | tests/auto/qml/qmltc_manual/testclasses.h | 22 | ||||
-rw-r--r-- | tests/auto/qml/qmltc_manual/tst_qmltc_manual.cpp | 39 |
2 files changed, 20 insertions, 41 deletions
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 <private/qqmlrefcount_p.h> #include <private/qqmlcontextdata_p.h> +#include <private/qqmlengine_p.h> // 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<QQmlContextData> create(QQmlEngine *engine, const QUrl &url, - const QQmlRefPointer<QQmlContextData> &parentContext, int index); + const QQmlRefPointer<QQmlContextData> &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<QQmlContextData> &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<QQmlContextData> -ContextRegistrator::create(QQmlEngine *engine, const QUrl &url, - const QQmlRefPointer<QQmlContextData> &parentContext, int index) -{ - Q_ASSERT(index >= 0); - QQmlRefPointer<QQmlContextData> 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<QQmlContextData> &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<quint32>(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) { |