diff options
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 10 | ||||
-rw-r--r-- | tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 12 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp index c147feeb73..1371f1f041 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp @@ -1119,6 +1119,11 @@ ReturnedValue QtObject::method_createQmlObject(const FunctionObject *b, const Va QQmlEngine *engine = scope.engine->qmlEngine(); QQmlContextData *context = scope.engine->callingQmlContext(); + if (!context) { + QQmlEngine *qmlEngine = scope.engine->qmlEngine(); + if (qmlEngine) + context = QQmlContextData::get(QQmlEnginePrivate::get(qmlEngine)->rootContext); + } Q_ASSERT(context); QQmlContext *effectiveContext = nullptr; if (context->isPragmaLibraryContext) @@ -1246,6 +1251,11 @@ ReturnedValue QtObject::method_createComponent(const FunctionObject *b, const Va QQmlEngine *engine = scope.engine->qmlEngine(); QQmlContextData *context = scope.engine->callingQmlContext(); + if (!context) { + QQmlEngine *qmlEngine = scope.engine->qmlEngine(); + if (qmlEngine) + context = QQmlContextData::get(QQmlEnginePrivate::get(qmlEngine)->rootContext); + } Q_ASSERT(context); QQmlContextData *effectiveContext = context; if (context->isPragmaLibraryContext) diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp index bf964a5744..52e18011cb 100644 --- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp +++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp @@ -76,6 +76,7 @@ private slots: void qmlContextProperties(); void testGCCorruption(); void testGroupedPropertyRevisions(); + void componentFromEval(); public slots: QObject *createAQObjectForOwnershipTest () @@ -885,6 +886,17 @@ void tst_qqmlengine::testGroupedPropertyRevisions() QVERIFY(!c2.errorString().isEmpty()); } +void tst_qqmlengine::componentFromEval() +{ + QQmlEngine engine; + const QUrl testUrl = testFileUrl("EmptyComponent.qml"); + QJSValue result = engine.evaluate("Qt.createComponent(\"" + testUrl.toString() + "\");"); + QPointer<QQmlComponent> component(qobject_cast<QQmlComponent*>(result.toQObject())); + QVERIFY(!component.isNull()); + QScopedPointer<QObject> item(component->create()); + QVERIFY(!item.isNull()); +} + QTEST_MAIN(tst_qqmlengine) #include "tst_qqmlengine.moc" |