diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-05-03 11:48:56 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-05-23 21:25:46 +0200 |
commit | 0d97723ee2778203af0cfd13599572110fb69dd5 (patch) | |
tree | de1562da4e76010f6513d3ee4d80f0a867386b3c /src/widgets/kernel | |
parent | 7ec215301612e7d52352738629d64360c48baf28 (diff) |
Fix separate delete of window and windowcontainer
The documentation says we can change window parent to avoid the widget
deleting the window. That didn't work as the widget didn't get the
child-removed event as it wasn't the parent.
This patch instead uses an event filter on the set parent.
Pick-to: 6.3 6.2
Change-Id: I1f61d1832fcf3257722f305beeefd8f1abf1f656
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
Diffstat (limited to 'src/widgets/kernel')
-rw-r--r-- | src/widgets/kernel/qwindowcontainer.cpp | 35 | ||||
-rw-r--r-- | src/widgets/kernel/qwindowcontainer_p.h | 1 |
2 files changed, 30 insertions, 6 deletions
diff --git a/src/widgets/kernel/qwindowcontainer.cpp b/src/widgets/kernel/qwindowcontainer.cpp index 847058e5b2..072a0bc5fa 100644 --- a/src/widgets/kernel/qwindowcontainer.cpp +++ b/src/widgets/kernel/qwindowcontainer.cpp @@ -191,6 +191,7 @@ QWindowContainer::QWindowContainer(QWindow *embeddedWindow, QWidget *parent, Qt: d->fakeParent.setObjectName(windowName + "ContainerFakeParent"_L1); d->window->setParent(&d->fakeParent); + d->window->parent()->installEventFilter(this); d->window->setFlag(Qt::SubWindow); setAcceptDrops(true); @@ -243,6 +244,26 @@ void QWindowContainer::focusWindowChanged(QWindow *focusWindow) \internal */ +bool QWindowContainer::eventFilter(QObject *o, QEvent *e) +{ + Q_D(QWindowContainer); + if (!d->window) + return false; + + if (e->type() == QEvent::ChildRemoved) { + QChildEvent *ce = static_cast<QChildEvent *>(e); + if (ce->child() == d->window) { + o->removeEventFilter(this); + d->window = nullptr; + } + } + return false; +} + +/*! + \internal + */ + bool QWindowContainer::event(QEvent *e) { Q_D(QWindowContainer); @@ -251,12 +272,6 @@ bool QWindowContainer::event(QEvent *e) QEvent::Type type = e->type(); switch (type) { - case QEvent::ChildRemoved: { - QChildEvent *ce = static_cast<QChildEvent *>(e); - if (ce->child() == d->window) - d->window = nullptr; - break; - } // The only thing we are interested in is making sure our sizes stay // in sync, so do a catch-all case. case QEvent::Resize: @@ -271,10 +286,13 @@ bool QWindowContainer::event(QEvent *e) case QEvent::Show: d->updateUsesNativeWidgets(); if (d->isStillAnOrphan()) { + d->window->parent()->removeEventFilter(this); d->window->setParent(d->usesNativeWidgets ? windowHandle() : window()->windowHandle()); d->fakeParent.destroy(); + if (d->window->parent()) + d->window->parent()->installEventFilter(this); } if (d->window->parent()) { d->markParentChain(); @@ -345,7 +363,10 @@ static void qwindowcontainer_traverse(QWidget *parent, qwindowcontainer_traverse void QWindowContainer::toplevelAboutToBeDestroyed(QWidget *parent) { if (QWindowContainerPrivate *d = QWindowContainerPrivate::get(parent)) { + if (d->window->parent()) + d->window->parent()->removeEventFilter(parent); d->window->setParent(&d->fakeParent); + d->window->parent()->installEventFilter(parent); } qwindowcontainer_traverse(parent, toplevelAboutToBeDestroyed); } @@ -363,7 +384,9 @@ void QWindowContainer::parentWasChanged(QWidget *parent) tld->createTLSysExtra(); Q_ASSERT(toplevel->windowHandle()); } + d->window->parent()->removeEventFilter(parent); d->window->setParent(toplevel->windowHandle()); + toplevel->windowHandle()->installEventFilter(parent); d->fakeParent.destroy(); d->updateGeometry(); } diff --git a/src/widgets/kernel/qwindowcontainer_p.h b/src/widgets/kernel/qwindowcontainer_p.h index e8944191e6..8dc5c64af4 100644 --- a/src/widgets/kernel/qwindowcontainer_p.h +++ b/src/widgets/kernel/qwindowcontainer_p.h @@ -40,6 +40,7 @@ public: protected: bool event(QEvent *ev) override; + bool eventFilter(QObject *, QEvent *ev) override; private slots: void focusWindowChanged(QWindow *focusWindow); |