diff options
author | Axel Spoerl <axel.spoerl@qt.io> | 2023-05-10 10:14:20 +0200 |
---|---|---|
committer | Axel Spoerl <axel.spoerl@qt.io> | 2023-06-21 00:30:41 +0200 |
commit | f83ea896227dfb37281ca18cdebbd072df3b1da7 (patch) | |
tree | a082638a4f65c893f1a8b0aec6d178600b6be798 | |
parent | 3f72b0d5fc70d3cf7daa4badccd5a40fc8b0726a (diff) |
Implement QWidgetPrivate::focusObject()
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
Pick-to: 6.6 6.5 6.2
Done-With: Liang Qi <liang.qi@qt.io>
Change-Id: Icf01e8ac4fc5f722fbf8e0ca5a562617ae9ae8f2
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Liang Qi <liang.qi@qt.io>
-rw-r--r-- | src/widgets/kernel/qwidget.cpp | 7 | ||||
-rw-r--r-- | src/widgets/kernel/qwidget_p.h | 2 | ||||
-rw-r--r-- | tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp | 1 |
3 files changed, 9 insertions, 1 deletions
diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index 6ef8b76672..e57aba3a56 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -6853,6 +6853,13 @@ QWidget *QWidget::focusWidget() const return const_cast<QWidget *>(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 2e1c4030f8..07153e7129 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 030bb1cf5b..7a2b500418 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'); |