aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlguard_p.h
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2018-05-07 11:45:09 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2018-05-11 09:40:11 +0000
commit54da8f6c86aa9cba99bcedcd58b40db938b5af9e (patch)
treeaf3569a9da0063abcdd136e18702ea8042ee7f02 /src/qml/qml/qqmlguard_p.h
parent2dd213c34b5ba90cd811fada5b0c4494171abe46 (diff)
Fix JS ownership of model and delegate properties in QtQuick item views
When assigning a JS owned model or delegate to an item view, we must ensure that they stay alive as long as the item view. This happens easily for example when doing something like delegate: Qt.createComponent(...) This patch takes the minimally invasive approach by changing the QObject parent of such objects. Task-number: QTBUG-50319 Task-number: QTBUG-51620 Change-Id: Ie6384b8dd93dcdc62d49f64b38173b3fc4ffd3b3 Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlguard_p.h')
-rw-r--r--src/qml/qml/qqmlguard_p.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlguard_p.h b/src/qml/qml/qqmlguard_p.h
index 808bf4c709..3ac63926a0 100644
--- a/src/qml/qml/qqmlguard_p.h
+++ b/src/qml/qml/qqmlguard_p.h
@@ -106,6 +106,34 @@ protected:
virtual void objectDestroyed(T *) {}
};
+template <typename T>
+class QQmlStrongJSQObjectReference : public QQmlGuard<T>
+{
+public:
+ void setObject(T *o, QObject *parent) {
+ T *old = this->object();
+ if (o == old)
+ return;
+
+ if (m_jsOwnership && old && old->parent() == parent)
+ QQml_setParent_noEvent(old, nullptr);
+
+ this->QQmlGuard<T>::operator=(o);
+
+ if (o && !o->parent() && !QQmlData::keepAliveDuringGarbageCollection(o)) {
+ m_jsOwnership = true;
+ QQml_setParent_noEvent(o, parent);
+ } else {
+ m_jsOwnership = false;
+ }
+ }
+
+private:
+ using QQmlGuard<T>::setObject;
+ using QQmlGuard<T>::operator=;
+ bool m_jsOwnership = false;
+};
+
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QQmlGuard<QObject>)