From f44ef9daf9f0f9db6775fdb6d3fc8703b6ce77e4 Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Mon, 1 Aug 2016 19:01:03 +0200 Subject: macOS: Fix native dangling menu still visible on screen and crash If the parent window gets destroyed while a QtQuick Controls menu is open the macOS native platform menu is not dismissed and you see a blank gray rectangle without any text. Also, at this point the QQmlEngine was already destroyed but it's still present on the call stack, so you get a crash when the stack unwinds to the original right mouse click that created the context menu. Change-Id: I638b0de13734815995d2994e6dd6603bcb0ebefc Reviewed-by: Gabriel de Dietrich --- src/controls/qquickmenu.cpp | 17 +++++++++++++++++ src/controls/qquickmenu_p.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/controls/qquickmenu.cpp b/src/controls/qquickmenu.cpp index b725bbbf..3c243ba3 100644 --- a/src/controls/qquickmenu.cpp +++ b/src/controls/qquickmenu.cpp @@ -436,6 +436,10 @@ void QQuickMenu1::__popup(const QRectF &targetRect, int atItemIndex, MenuType me // parentWindow may not be a QQuickWindow (happens when using QQuickWidget) if (m_platformMenu) { + if (m_windowConnection) + QObject::disconnect(m_windowConnection); + m_windowConnection = connect(parentWindow, &QWindow::visibleChanged, this, + &QQuickMenu1::platformMenuWindowVisibleChanged, Qt::UniqueConnection); QRectF globalTargetRect = targetRect.translated(m_xOffset, m_yOffset); if (visualItem()) { if (qGuiApp->isRightToLeft()) { @@ -565,6 +569,19 @@ void QQuickMenu1::windowVisibleChanged(bool v) } } +void QQuickMenu1::platformMenuWindowVisibleChanged(bool visible) +{ + if (!visible) { + if (m_windowConnection) { + QObject::disconnect(m_windowConnection); + m_windowConnection = QMetaObject::Connection(); + } + if (m_platformMenu) { + m_platformMenu->dismiss(); + } + } +} + void QQuickMenu1::clearPopupWindow() { m_popupWindow = 0; diff --git a/src/controls/qquickmenu_p.h b/src/controls/qquickmenu_p.h index 576ae5fd..8510efbe 100644 --- a/src/controls/qquickmenu_p.h +++ b/src/controls/qquickmenu_p.h @@ -165,6 +165,7 @@ protected Q_SLOTS: void updateText(); void windowVisibleChanged(bool); + void platformMenuWindowVisibleChanged(bool); private: QQuickWindow *findParentWindow(); @@ -205,6 +206,7 @@ private: QFont m_font; int m_triggerCount; bool m_proxy; + QMetaObject::Connection m_windowConnection; }; QT_END_NAMESPACE -- cgit v1.2.1