diff options
author | Erik Verbruggen <erik.verbruggen@qt.io> | 2018-03-08 15:02:45 +0100 |
---|---|---|
committer | Erik Verbruggen <erik.verbruggen@qt.io> | 2018-03-16 08:52:17 +0000 |
commit | 3339bf866a68ba129e88fd7d27a2abbea0d1c6ba (patch) | |
tree | 06a90f7a146b7f2bb6eadd03dded4057f6eb4a71 /src | |
parent | 928a5d40377853aafbb2860feccf6d65bf81417b (diff) |
When deactivating a loader, do not immediately clear its context
In 2eb2d6386da304cd1164264ae0bff685c796d89c, deactivating/clearing the
loader would now prevent any subsequent bindings from being evaluated.
The problem there was that the item created by the loader wouldn't have
a parent item (among things) anymore, so references to it in the
bindings would result in errors.
The way to prevent it was done by invalidating the context of the item,
which in turn would detach it from the root context. This is a problem
if objects in the root context are referenced after
deactivating/clearing the loader:
onSomethingChanged: {
loader.source = ""
objectInRootContext.doIt()
}
This would result in a ReferenceError when resolving objectInRootContext
and break the behavior present before the fix mentioned above. The
correct way is to recursively clear the context set on all bindings, but
leave everything in place. This way, no subsequent bindings will be
evaluated, but the currently "running" scripts will still be able to
reach the root context.
Task-number: QTBUG-66822
Change-Id: Ic9c2ab0a752093a26967da4783cb4c29cf83d2ca
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Michael Brasser <michael.brasser@live.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/qml/qqmlcontext.cpp | 8 | ||||
-rw-r--r-- | src/qml/qml/qqmlcontext_p.h | 1 | ||||
-rw-r--r-- | src/quick/items/qquickloader.cpp | 4 |
3 files changed, 11 insertions, 2 deletions
diff --git a/src/qml/qml/qqmlcontext.cpp b/src/qml/qml/qqmlcontext.cpp index 6e43bc735f..5dd3278b4c 100644 --- a/src/qml/qml/qqmlcontext.cpp +++ b/src/qml/qml/qqmlcontext.cpp @@ -593,6 +593,14 @@ void QQmlContextData::invalidate() parent = nullptr; } +void QQmlContextData::clearContextRecursively() +{ + clearContext(); + + for (auto ctxIt = childContexts; ctxIt; ctxIt = ctxIt->nextChild) + ctxIt->clearContextRecursively(); +} + void QQmlContextData::clearContext() { emitDestruction(); diff --git a/src/qml/qml/qqmlcontext_p.h b/src/qml/qml/qqmlcontext_p.h index ff36d6c9a8..5dfee48848 100644 --- a/src/qml/qml/qqmlcontext_p.h +++ b/src/qml/qml/qqmlcontext_p.h @@ -115,6 +115,7 @@ public: QQmlContextData(QQmlContext *); void emitDestruction(); void clearContext(); + void clearContextRecursively(); void invalidate(); inline bool isValid() const { diff --git a/src/quick/items/qquickloader.cpp b/src/quick/items/qquickloader.cpp index 34f30e81a3..6960e16bd9 100644 --- a/src/quick/items/qquickloader.cpp +++ b/src/quick/items/qquickloader.cpp @@ -102,7 +102,7 @@ void QQuickLoaderPrivate::clear() // this we may get transient errors from use of 'parent', for example. QQmlContext *context = qmlContext(object); if (context) - QQmlContextData::get(context)->invalidate(); + QQmlContextData::get(context)->clearContextRecursively(); if (loadingFromSource && component) { // disconnect since we deleteLater @@ -363,7 +363,7 @@ void QQuickLoader::setActive(bool newVal) // this we may get transient errors from use of 'parent', for example. QQmlContext *context = qmlContext(d->object); if (context) - QQmlContextData::get(context)->invalidate(); + QQmlContextData::get(context)->clearContextRecursively(); if (d->item) { QQuickItemPrivate *p = QQuickItemPrivate::get(d->item); |