From 0544c2b41fdb0a7d7efc9db701bac561e0edf1f3 Mon Sep 17 00:00:00 2001
From: Andy Shaw <andy.shaw@qt.io>
Date: Mon, 15 May 2017 08:21:00 +0200
Subject: [PATCH] Unset widget window's parent when WM_DESTROY is sent to
 prevent a crash

When a control is activated and deactivated in Powerpoint then it will
cause the ActiveX window hosting the QWidget to be destroyed. In this
case we need to set the parent of this widget's own QWindow's HWND to
be 0 so it does not reference the deleted window.

This reflects the handling in WM_SHOWWINDOW where the parent is set for
the widget's own QWindow's HWND.

Change-Id: Ie32d408782cdf94a98cb13eda49f6ccf73cccd51
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
---
 src/activeqt/control/qaxserverbase.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/activeqt/control/qaxserverbase.cpp b/src/activeqt/control/qaxserverbase.cpp
index f2d42624..9efded27 100644
--- a/src/activeqt/control/qaxserverbase.cpp
+++ b/src/activeqt/control/qaxserverbase.cpp
@@ -1400,8 +1400,13 @@ LRESULT QT_WIN_CALLBACK QAxServerBase::ActiveXProc(HWND hWnd, UINT uMsg, WPARAM
     case WM_QUERYENDSESSION:
     case WM_DESTROY:
         if (QAxServerBase *that = axServerBaseFromWindow(hWnd)) {
-            if (that->qt.widget)
+            if (that->qt.widget) {
                 that->qt.widget->hide();
+                if (QWindow *widgetWindow = that->qt.widget->windowHandle()) {
+                    if (HWND h = reinterpret_cast<HWND>(widgetWindow->winId()))
+                        ::SetParent(h, 0);
+                }
+            }
         }
         break;
 
-- 
GitLab