diff options
Diffstat (limited to 'src/qml/qml/qqmlcontext.cpp')
-rw-r--r-- | src/qml/qml/qqmlcontext.cpp | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/src/qml/qml/qqmlcontext.cpp b/src/qml/qml/qqmlcontext.cpp index 3710cee162..f75a076bcb 100644 --- a/src/qml/qml/qqmlcontext.cpp +++ b/src/qml/qml/qqmlcontext.cpp @@ -317,6 +317,12 @@ void QQmlContext::setContextProperty(const QString &name, const QVariant &value) d->propertyValues[idx] = value; QMetaObject::activate(this, d->notifyIndex, idx, nullptr); } + + if (auto *obj = qvariant_cast<QObject *>(value)) { + connect(obj, &QObject::destroyed, this, [d, name](QObject *destroyed) { + d->dropDestroyedQObject(name, destroyed); + }); + } } /*! @@ -352,7 +358,7 @@ void QQmlContext::setContextProperties(const QVector<PropertyPair> &properties) data->expressions = nullptr; data->childContexts = nullptr; - for (auto property : properties) + for (const auto &property : properties) setContextProperty(property.name, property.value); data->expressions = expressions; @@ -438,23 +444,20 @@ QUrl QQmlContext::resolvedUrl(const QUrl &src) QUrl QQmlContextData::resolvedUrl(const QUrl &src) { - QQmlContextData *ctxt = this; - QUrl resolved; if (src.isRelative() && !src.isEmpty()) { - if (ctxt) { - while(ctxt) { - if (ctxt->url().isValid()) - break; - else - ctxt = ctxt->parent; - } - - if (ctxt) - resolved = ctxt->url().resolved(src); - else if (engine) - resolved = engine->baseUrl().resolved(src); - } + QQmlContextData *ctxt = this; + do { + if (ctxt->url().isValid()) + break; + else + ctxt = ctxt->parent; + } while (ctxt); + + if (ctxt) + resolved = ctxt->url().resolved(src); + else if (engine) + resolved = engine->baseUrl().resolved(src); } else { resolved = src; } @@ -527,6 +530,20 @@ QObject *QQmlContextPrivate::context_at(QQmlListProperty<QObject> *prop, int ind } } +void QQmlContextPrivate::dropDestroyedQObject(const QString &name, QObject *destroyed) +{ + if (!data->isValid()) + return; + + const int idx = data->propertyNames().value(name); + Q_ASSERT(idx >= 0); + if (qvariant_cast<QObject *>(propertyValues[idx]) != destroyed) + return; + + propertyValues[idx] = QVariant::fromValue<QObject *>(nullptr); + QMetaObject::activate(q_func(), notifyIndex, idx, nullptr); +} + QQmlContextData::QQmlContextData() : QQmlContextData(nullptr) @@ -845,7 +862,7 @@ QQmlContextPrivate *QQmlContextData::asQQmlContextPrivate() return QQmlContextPrivate::get(asQQmlContext()); } -void QQmlContextData::initFromTypeCompilationUnit(const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit, int subComponentIndex) +void QQmlContextData::initFromTypeCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit, int subComponentIndex) { typeCompilationUnit = unit; componentObjectIndex = subComponentIndex == -1 ? /*root object*/0 : subComponentIndex; |