diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-03-17 12:56:01 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-03-25 17:04:37 +0100 |
commit | 71b3d18ea7962a01413966f793e8187c9e91685e (patch) | |
tree | 1018dddeeac741e58c939159bdffdd4743796ca5 /src/gui/kernel | |
parent | d6919b073aaae617f1ff37d18da14e315f202005 (diff) |
Notify about focus object changes upon widget destruction
If the active QWidget gets destroyed, then QWidgetWindow::focusObject
will return nullptr. If then no other object takes focus, then we'd
never emit change signals, and QGuiApplication's _q_updateFocusObject
(which then informs the input context and emits signals) didn't get
called. This left the input context with a dangling focus object
pointer, which resulted in crashes.
If the QWidget clears its focus, but the corresponding window doesn't
know that it had focus, then fall back to the widget's focus widget
to see if we have a change in focus, so that signals get emitted.
Add a test case that shows that we didn't call _q_updateFocusObject
by counting emissions of the QGuiApplication::focusObjectChanged
signal, which we emit in this function. The signal is emitted more
than once both when showing a widget, and now also when destroying
a widget that has a focus child. The former is a previous issue,
the latter is an improvement to not emitting the signal at all.
Pick-to: 6.3 6.2
Fixes: QTBUG-101423
Fixes: QTBUG-101321
Change-Id: Ib96a397211d442f52ce795a3eebd055a0ef51b0d
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/gui/kernel')
-rw-r--r-- | src/gui/kernel/qguiapplication.cpp | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 883b22b7ed..0935f654eb 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -2498,8 +2498,15 @@ void QGuiApplicationPrivate::processActivatedEvent(QWindowSystemInterfacePrivate if (self) { self->notifyActiveWindowChange(previous); - if (previousFocusObject != qApp->focusObject()) + if (previousFocusObject != qApp->focusObject() || + // We are getting an activation change but there is no new focusObject, and we also + // don't have a previousFocusObject in the previously active window anymore. This can + // happen when window gets destroyed (see QWidgetWindow::focusObject returning nullptr + // when already in the QWidget destructor), so update the focusObject to avoid dangling + // pointers. See also QWidget::clearFocus(), which tries to cover for this as well. + (previous && previousFocusObject == nullptr && qApp->focusObject() == nullptr)) { self->_q_updateFocusObject(qApp->focusObject()); + } } emit qApp->focusWindowChanged(newFocus); |