aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-02-28 15:28:34 +0100
committerUlf Hermann <ulf.hermann@qt.io>2022-03-21 10:50:29 +0100
commitd76efbe8efab30eddbdf48b77fbbec712e2e7652 (patch)
treed1fdcbb47a3df913ba2baf2fe494a2ca9f6ee232 /src/quick/items
parenta5635345175e667601c8b6a344508c4d4ebb0f9d (diff)
QQuickLoader: Check for QQmlEngine before using it
The loader's context may have been removed from the context hierarchy or it may not have a context in the first place. We should not crash then. Fixes: QTBUG-67950 Change-Id: I1058d5b1f978aa040f8b2f018c4357dd7a3ef333 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit 79e885537f8546a18d7d9d902d6efe40b1915c96)
Diffstat (limited to 'src/quick/items')
-rw-r--r--src/quick/items/qquickloader.cpp26
-rw-r--r--src/quick/items/qquickloader_p_p.h1
2 files changed, 20 insertions, 7 deletions
diff --git a/src/quick/items/qquickloader.cpp b/src/quick/items/qquickloader.cpp
index 93cff95b7b..7b08ec5a66 100644
--- a/src/quick/items/qquickloader.cpp
+++ b/src/quick/items/qquickloader.cpp
@@ -439,9 +439,8 @@ void QQuickLoader::loadFromSource()
}
if (isComponentComplete()) {
- QQmlComponent::CompilationMode mode = d->asynchronous ? QQmlComponent::Asynchronous : QQmlComponent::PreferSynchronous;
if (!d->component)
- d->component.setObject(new QQmlComponent(qmlEngine(this), d->source, mode, this), this);
+ d->createComponent();
d->load();
}
}
@@ -806,11 +805,8 @@ void QQuickLoader::componentComplete()
Q_D(QQuickLoader);
QQuickItem::componentComplete();
if (active()) {
- if (d->loadingFromSource) {
- QQmlComponent::CompilationMode mode = d->asynchronous ? QQmlComponent::Asynchronous : QQmlComponent::PreferSynchronous;
- if (!d->component)
- d->component.setObject(new QQmlComponent(qmlEngine(this), d->source, mode, this), this);
- }
+ if (d->loadingFromSource && !d->component)
+ d->createComponent();
d->load();
}
}
@@ -1044,6 +1040,22 @@ void QQuickLoaderPrivate::updateStatus()
}
}
+void QQuickLoaderPrivate::createComponent()
+{
+ Q_Q(QQuickLoader);
+ const QQmlComponent::CompilationMode mode = asynchronous
+ ? QQmlComponent::Asynchronous
+ : QQmlComponent::PreferSynchronous;
+ if (QQmlContext *context = qmlContext(q)) {
+ if (QQmlEngine *engine = context->engine()) {
+ component.setObject(new QQmlComponent(engine, source, mode, q), q);
+ return;
+ }
+ }
+
+ qmlWarning(q) << "createComponent: Cannot find a QML engine.";
+}
+
#include <moc_qquickloader_p.cpp>
QT_END_NAMESPACE
diff --git a/src/quick/items/qquickloader_p_p.h b/src/quick/items/qquickloader_p_p.h
index 5fc231a8e6..4c63b82b6a 100644
--- a/src/quick/items/qquickloader_p_p.h
+++ b/src/quick/items/qquickloader_p_p.h
@@ -98,6 +98,7 @@ public:
QV4::ReturnedValue extractInitialPropertyValues(QQmlV4Function *args, QObject *loader, bool *error);
QQuickLoader::Status computeStatus() const;
void updateStatus();
+ void createComponent();
qreal getImplicitWidth() const override;
qreal getImplicitHeight() const override;