From 0a981bd9db44b2ab414321de5d7c93a5d2d7b7ed Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Wed, 10 May 2023 10:14:20 +0200 Subject: [PATCH] Implement QWidgetPrivate::focusObject() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QWidgetPrivate::focusObject() always returns nullptr. That has lead to mismatches between QGuiApplication::focusObject() and QApplication::focusWidget(), when a widget got focus by the window system (e.g. mouse click). This patch implements QWidgetPrivate::focusObject. It returns the current widget, if it doesn't have a focus proxy. If it has a focus proxy, it resolves the proxy chain and returns the deepest focus proxy. (Note: It does not return QWidget::focusWidget(), because the focus widget might not yet have been set, when the method is called). Fixes: QTBUG-92464 Fixes: QTBUG-108522 Done-With: Liang Qi Change-Id: Icf01e8ac4fc5f722fbf8e0ca5a562617ae9ae8f2 Reviewed-by: Volker Hilsheimer Reviewed-by: Tor Arne Vestbø Reviewed-by: Liang Qi (cherry picked from commit f83ea896227dfb37281ca18cdebbd072df3b1da7) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/kernel/qwidget.cpp | 7 +++++++ src/widgets/kernel/qwidget_p.h | 2 +- tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index 212ea3a1060..dda15779afe 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -6853,6 +6853,13 @@ QWidget *QWidget::focusWidget() const return const_cast(d_func()->focus_child); } +QObject *QWidgetPrivate::focusObject() +{ + Q_Q(QWidget); + QWidget *proxy = deepestFocusProxy(); + return proxy ? proxy : q; +} + /*! Returns the next widget in this widget's focus chain. diff --git a/src/widgets/kernel/qwidget_p.h b/src/widgets/kernel/qwidget_p.h index 2e1c4030f80..07153e71293 100644 --- a/src/widgets/kernel/qwidget_p.h +++ b/src/widgets/kernel/qwidget_p.h @@ -579,7 +579,7 @@ public: inline QRect mapFromWS(const QRect &r) const { return r.translated(data.wrect.topLeft()); } - virtual QObject *focusObject() { return nullptr; } + virtual QObject *focusObject(); virtual QPlatformBackingStoreRhiConfig rhiConfig() const { return {}; } diff --git a/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp b/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp index 030bb1cf5b3..7a2b500418f 100644 --- a/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp +++ b/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp @@ -1687,6 +1687,7 @@ void tst_QCompleter::QTBUG_14292_filesystem() QTRY_VERIFY(comp.popup()->isVisible()); QCOMPARE(comp.popup()->model()->rowCount(), 2); QApplication::processEvents(); + QCOMPARE(qApp->focusObject(), &edit); // for QTBUG_108522 QTest::keyClick(&edit, 'h'); QCOMPARE(comp.popup()->model()->rowCount(), 2); QTest::keyClick(&edit, 'e');