diff options
| author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2024-12-10 19:16:14 +0000 |
|---|---|---|
| committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-12-11 02:41:48 +0000 |
| commit | 7598dbad191639d33255d11fc4f928550025bce6 (patch) | |
| tree | 7f077fe6b966628de1bcc3d64ea293696fe41364 | |
| parent | 43923f44d16b438c40e86233a5e68ea4ba499681 (diff) | |
Revert "Make ApplicationWindow's content item the fallback focused item on startup"
This reverts commit 71f687111b45a1d391c05fa26ef1cc72c75c9781.
The application window's content item having focus on startup
prior to 83d90845527799c78366c2b89e9c14bc35e88695 was a side-
effect of the item being a focus scope. In that situation any
parented item into the content item with focus will keep its
focus if no other item in the content item has focus at that
point.
However in the new setup the application window's content item
is not a focus scope (the root QQuickWindow content item is),
so giving it focus will prevent parented items from keeping
their focus. The workaround in the reverted patch of setting
the focus on completion instead of classBegin only worked for
the simple case where the focused item is part of the initial
object hierarchy. If an item is inserted after the app window
is completed this would still fail.
Change-Id: I129d8d8a7c048222d1af220bc209b4c7f20b97ca
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 7a6e44c0dae24678c46fb46c7285a943afced3f4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
3 files changed, 17 insertions, 34 deletions
diff --git a/src/quicktemplates/qquickapplicationwindow.cpp b/src/quicktemplates/qquickapplicationwindow.cpp index f55b02497a..6e4e76a5ed 100644 --- a/src/quicktemplates/qquickapplicationwindow.cpp +++ b/src/quicktemplates/qquickapplicationwindow.cpp @@ -821,7 +821,10 @@ void QQuickApplicationWindow::classBegin() auto *contentItem = new QQuickContentItem(this, d->control); // The content item can't be its own focus scope here, as that // will detach focus of items inside the content item from focus - // in the menubar, header and footer. + // in the menubar, header and footer. Nor can set set the content + // item as focused, as that will prevent child items of the content + // item from getting the initial focus when they are reparented + // into the content item. d->control->setContentItem(contentItem); auto *context = qmlContext(this); @@ -852,13 +855,7 @@ void QQuickApplicationWindow::componentComplete() { Q_D(QQuickApplicationWindow); d->componentComplete = true; - contentItem()->setObjectName(QQmlMetaType::prettyTypeName(this)); - // If no other item has requested focus, make the app window's - // content item the focused item, matching the behavior from when - // the app window's content item was the root of the window and - // a focus scope. - if (!QQuickWindow::contentItem()->scopedFocusItem()) - contentItem()->setFocus(true); + QQuickWindow::contentItem()->setObjectName(QQmlMetaType::prettyTypeName(this)); d->executeBackground(true); QQuickWindowQmlImpl::componentComplete(); d->relayout(); diff --git a/tests/auto/quickcontrols/qquickapplicationwindow/data/defaultFocus.qml b/tests/auto/quickcontrols/qquickapplicationwindow/data/defaultFocus.qml index eb1f963887..68b889bdc2 100644 --- a/tests/auto/quickcontrols/qquickapplicationwindow/data/defaultFocus.qml +++ b/tests/auto/quickcontrols/qquickapplicationwindow/data/defaultFocus.qml @@ -9,13 +9,11 @@ ApplicationWindow { width: 200 height: 200 - required property bool itemFocused - property bool receivedKeyPress: false Item { objectName: "item" - focus: itemFocused + focus: true anchors.fill: parent Keys.onLeftPressed: receivedKeyPress = true diff --git a/tests/auto/quickcontrols/qquickapplicationwindow/tst_qquickapplicationwindow.cpp b/tests/auto/quickcontrols/qquickapplicationwindow/tst_qquickapplicationwindow.cpp index 721eb24528..84fee17326 100644 --- a/tests/auto/quickcontrols/qquickapplicationwindow/tst_qquickapplicationwindow.cpp +++ b/tests/auto/quickcontrols/qquickapplicationwindow/tst_qquickapplicationwindow.cpp @@ -5,7 +5,6 @@ #include <QtCore/qoperatingsystemversion.h> #include <QtTest/QSignalSpy> #include <QtQml/qqmlengine.h> -#include <QtQml/qqmlapplicationengine.h> #include <QtQml/qqmlcomponent.h> #include <QtQml/qqmlcontext.h> #include <QtQuick/qquickview.h> @@ -40,7 +39,6 @@ private slots: void qmlCreation(); void activeFocusOnTab1(); void activeFocusOnTab2(); - void defaultFocus_data(); void defaultFocus(); void implicitFill(); void attachedProperties(); @@ -209,22 +207,17 @@ void tst_QQuickApplicationWindow::activeFocusOnTab2() } } -void tst_QQuickApplicationWindow::defaultFocus_data() -{ - QTest::addColumn<bool>("itemFocused"); - QTest::newRow("no item focus") << false; - QTest::newRow("item focused") << true; -} - void tst_QQuickApplicationWindow::defaultFocus() { - QFETCH(bool, itemFocused); - - QQmlApplicationEngine engine; - engine.setInitialProperties({{ "itemFocused", itemFocused }}); - engine.load(testFileUrl("defaultFocus.qml")); + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("defaultFocus.qml")); + QObject* created = component.create(); + QScopedPointer<QObject> cleanup(created); + Q_UNUSED(cleanup); + QVERIFY(created); - QQuickWindow* window = qobject_cast<QQuickApplicationWindow *>(engine.rootObjects().value(0)); + QQuickWindow* window = qobject_cast<QQuickWindow*>(created); QVERIFY(window); window->show(); window->requestActivate(); @@ -235,16 +228,11 @@ void tst_QQuickApplicationWindow::defaultFocus() QVERIFY(contentItem); QVERIFY(contentItem->hasActiveFocus()); - auto* appWindow = qobject_cast<QQuickApplicationWindow*>(window); - auto* appWindowContentItem = appWindow->contentItem(); - QVERIFY(appWindowContentItem); - QCOMPARE(appWindowContentItem->hasFocus(), !itemFocused); - QCOMPARE(appWindowContentItem->hasActiveFocus(), !itemFocused); - + // A single item in an ApplicationWindow with focus: true should receive focus. QQuickItem* item = findItem<QQuickItem>(window->contentItem(), "item"); QVERIFY(item); - QCOMPARE(item->hasFocus(), itemFocused); - QCOMPARE(item->hasActiveFocus(), itemFocused); + QVERIFY(item->hasFocus()); + QVERIFY(item->hasActiveFocus()); } static QSizeF getExpectedElementSize() |
