diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2020-06-05 09:48:52 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2020-08-16 09:19:35 +0000 |
commit | 7810b97febd0bfe00132dec785c21ca4a361c818 (patch) | |
tree | 192f49ab20deda7d9c7956acf08bb2006147f597 /src | |
parent | 99c5fbd489292e7cea1bd3cf7585923440a5eae0 (diff) |
Fix crash on exit when centering Popup within Overlay in StackView item
If a Popup is centered within an Overlay, and that Overlay is destroyed
before the Popup, we must make sure centerIn is cleared so we don't try
to access a dangling pointer.
Fixes: QTBUG-84579
Change-Id: Icb2750f847f9d5710725bedc4d1c92bf4c122c03
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 04901b170e009d844232067446fdd56f270d155d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/quicktemplates2/qquickpopupanchors.cpp | 26 | ||||
-rw-r--r-- | src/quicktemplates2/qquickpopupanchors_p.h | 6 |
2 files changed, 31 insertions, 1 deletions
diff --git a/src/quicktemplates2/qquickpopupanchors.cpp b/src/quicktemplates2/qquickpopupanchors.cpp index 5acc2934..c7ac038d 100644 --- a/src/quicktemplates2/qquickpopupanchors.cpp +++ b/src/quicktemplates2/qquickpopupanchors.cpp @@ -47,6 +47,15 @@ QQuickPopupAnchors::QQuickPopupAnchors(QQuickPopup *popup) d->popup = popup; } +QQuickPopupAnchors::~QQuickPopupAnchors() +{ + Q_D(const QQuickPopupAnchors); + if (d->centerIn) { + auto centerInPrivate = QQuickItemPrivate::get(d->centerIn); + centerInPrivate->removeItemChangeListener(this, QQuickItemPrivate::Destroyed); + } +} + QQuickItem *QQuickPopupAnchors::centerIn() const { Q_D(const QQuickPopupAnchors); @@ -59,8 +68,20 @@ void QQuickPopupAnchors::setCenterIn(QQuickItem *item) if (item == d->centerIn) return; + if (d->centerIn) { + auto centerInPrivate = QQuickItemPrivate::get(d->centerIn); + centerInPrivate->removeItemChangeListener(this, QQuickItemPrivate::Destroyed); + } + d->centerIn = item; + + if (d->centerIn) { + auto centerInPrivate = QQuickItemPrivate::get(d->centerIn); + centerInPrivate->addItemChangeListener(this, QQuickItemPrivate::Destroyed); + } + QQuickPopupPrivate::get(d->popup)->reposition(); + emit centerInChanged(); } @@ -69,4 +90,9 @@ void QQuickPopupAnchors::resetCenterIn() setCenterIn(nullptr); } +void QQuickPopupAnchors::itemDestroyed(QQuickItem *) +{ + resetCenterIn(); +} + QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquickpopupanchors_p.h b/src/quicktemplates2/qquickpopupanchors_p.h index 531c494e..eff4e23d 100644 --- a/src/quicktemplates2/qquickpopupanchors_p.h +++ b/src/quicktemplates2/qquickpopupanchors_p.h @@ -50,6 +50,7 @@ #include <QtCore/qobject.h> #include <QtQml/qqml.h> +#include <QtQuick/private/qquickitem_p.h> #include <QtQuickTemplates2/private/qtquicktemplates2global_p.h> QT_BEGIN_NAMESPACE @@ -58,13 +59,14 @@ class QQuickItem; class QQuickPopupAnchorsPrivate; class QQuickPopup; -class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickPopupAnchors : public QObject +class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickPopupAnchors : public QObject, public QQuickItemChangeListener { Q_OBJECT Q_PROPERTY(QQuickItem *centerIn READ centerIn WRITE setCenterIn RESET resetCenterIn NOTIFY centerInChanged) public: explicit QQuickPopupAnchors(QQuickPopup *popup); + ~QQuickPopupAnchors(); QQuickItem *centerIn() const; void setCenterIn(QQuickItem *item); @@ -74,6 +76,8 @@ Q_SIGNALS: void centerInChanged(); private: + void itemDestroyed(QQuickItem *item) override; + Q_DISABLE_COPY(QQuickPopupAnchors) Q_DECLARE_PRIVATE(QQuickPopupAnchors) }; |