summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2021-01-13 11:41:41 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-01-15 19:28:52 +0000
commitd96f00286cd570d70d9e8829e665a38928aefc20 (patch)
treebcd3cd0e7cf73aac39791c56c3a81e07fee9f168
parenta24816ade4d18a3d04d5b0cca0f67b4c1eadbb2e (diff)
Fix setting active window as application's focus widget
When setting the application's focus widget we search for the next child widget that can hold the focus and call its setFocus() method, which also updates focus widgets of all its parent wigets. In case if the focus widget is the active window itself, we only set it as the application's focus widget, but we don't update the focus widget of the active window itself. Because of this the focusWidget() method always results nullptr for the active window. This prevents from setting the focus back to active window after the focus has changed (for example after a context menu is closed, as in the bugreport). Transfer the focus to active window by calling the setFocus() method, as it is done in case of transferring the focus to any other widget. Fixes: QTBUG-85846 Change-Id: I91ebf182fd5bb7d451a1186e2f3e38c8d48acc4e Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 154573929a76d9051920756b19ed033c4b2ac649) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/widgets/kernel/qapplication.cpp9
-rw-r--r--tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp40
2 files changed, 45 insertions, 4 deletions
diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp
index 63c3f9dd9b..b1341d1015 100644
--- a/src/widgets/kernel/qapplication.cpp
+++ b/src/widgets/kernel/qapplication.cpp
@@ -1869,12 +1869,13 @@ void QApplication::setActiveWindow(QWidget* act)
if (w) {
w->setFocus(Qt::ActiveWindowFocusReason);
} else {
- // If the focus widget is not in the activate_window, clear the focus
w = QApplicationPrivate::focus_widget;
- if (!w && QApplicationPrivate::active_window->focusPolicy() != Qt::NoFocus)
- QApplicationPrivate::setFocusWidget(QApplicationPrivate::active_window, Qt::ActiveWindowFocusReason);
- else if (!QApplicationPrivate::active_window->isAncestorOf(w))
+ if (!w && QApplicationPrivate::active_window->focusPolicy() != Qt::NoFocus) {
+ QApplicationPrivate::active_window->setFocus(Qt::ActiveWindowFocusReason);
+ } else if (!QApplicationPrivate::active_window->isAncestorOf(w)) {
+ // If the focus widget is not in the activate_window, clear the focus
QApplicationPrivate::setFocusWidget(nullptr, Qt::ActiveWindowFocusReason);
+ }
}
}
}
diff --git a/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp b/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp
index 750810724c..0aa915cc47 100644
--- a/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp
+++ b/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp
@@ -58,6 +58,7 @@
#include <QtWidgets/private/qapplication_p.h>
#include <QtWidgets/QStyle>
#include <QtWidgets/qproxystyle.h>
+#include <QtWidgets/QTextEdit>
#include <qpa/qwindowsysteminterface.h>
#include <qpa/qwindowsysteminterface_p.h>
@@ -120,6 +121,7 @@ private slots:
void setActiveWindow();
+ void focusWidget();
void focusChanged();
void focusOut();
void focusMouseClick();
@@ -1551,6 +1553,44 @@ void tst_QApplication::setActiveWindow()
delete w;
}
+void tst_QApplication::focusWidget()
+{
+ int argc = 0;
+ QApplication app(argc, nullptr);
+
+ // The focus widget is the active window itself
+ {
+ QTextEdit te;
+ te.show();
+
+ QApplication::setActiveWindow(&te);
+ QVERIFY(QTest::qWaitForWindowActive(&te));
+
+ const auto focusWidget = QApplication::focusWidget();
+ QVERIFY(focusWidget);
+ QVERIFY(focusWidget->hasFocus());
+ QVERIFY(te.hasFocus());
+ QCOMPARE(focusWidget, te.focusWidget());
+ }
+
+ // The focus widget is a child of the active window
+ {
+ QWidget w;
+ QTextEdit te(&w);
+ w.show();
+
+ QApplication::setActiveWindow(&w);
+ QVERIFY(QTest::qWaitForWindowActive(&w));
+
+ const auto focusWidget = QApplication::focusWidget();
+ QVERIFY(focusWidget);
+ QVERIFY(focusWidget->hasFocus());
+ QVERIFY(!w.hasFocus());
+ QVERIFY(te.hasFocus());
+ QCOMPARE(te.focusWidget(), w.focusWidget());
+ QCOMPARE(focusWidget, w.focusWidget());
+ }
+}
/* This might fail on some X11 window managers? */
void tst_QApplication::focusChanged()