Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.12.4
Description
Appears to be very similar to QTBUG-53275 except with non-ApplicationWindow (e.g. "just") Window objects. I appreciate that the patch released for that bug seemed to have some consideration for non-ApplicationWindow types:
else if (window) window->contentItem()->setFocus(true);
However I currently do not have Qt 5.7 to confirm whether this is a regression for this case... though the example code to reproduce is similar to the original bug - with workaround QML code commented at the bottom which (provided the imports are these newer versions compared to the original) can access the event with which the overlay is no longer visible to reset the focus:
import QtQuick 2.12 import QtQuick.Layouts 1.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { id: window visible: true width: 400 height: 200 onActiveFocusItemChanged: print("activeFocusItem", activeFocusItem) FocusScope { id: focusScope focus: true anchors.fill: parent objectName: "test" onFocusChanged: print("focus", focus) property bool toggled: false onToggledChanged: print("toggled", toggled) Keys.onPressed: { if (event.modifiers === Qt.AltModifier) { focusScope.toggled = true; } } Keys.onReleased: { if ((event.modifiers === Qt.AltModifier || event.key === Qt.Key_Alt) && !event.isAutoRepeat) { focusScope.toggled = false; } } RowLayout { anchors.centerIn: parent Button { text: qsTr("File") onClicked: fileMenu2.open() focusPolicy: Qt.NoFocus Menu { id: fileMenu2 y: parent.height MenuItem { text: qsTr("New") } MenuItem { text: qsTr("open") } MenuItem { text: qsTr("Close") } } } Button { id: penButton text: qsTr("Pen") highlighted: !focusScope.toggled } Button { id: eyedropperButton text: qsTr("Eyedropper") highlighted: focusScope.toggled } } } /* KLUDGE: Popup items will give focus to sibling overlay - we need to * restore keyboard bindings to focusScope when the popup(s) are gone * i.e. when overlay is invisible. Connections { target: window.Overlay.overlay onVisibleChanged: { if (!window.Overlay.overlay.visible) { focusScope.focus = true; } } } */ }
The repro steps are pretty much identical to QTBUG-53275 except now the controls are in a regular Window instead of ApplicationWindow.
The idea is that holding the Alt key down will toggle between two "modes". The mode is indicated by a highlighted button.
Try switching between the buttons. Now open the menu by clicking the "File" button. The menu will receive focus and the focus scope will lose it. If you then close the menu and try to switch between the buttons again, it won't work because the focus scope never regained focus (the root item now has it).
To disambiguate a bit further from the description of QTBUG-53275 - the "root item" that holds focus afterwards I can see is indeed the Window.contentItem }}but this is the "invisible root item of the scene" - not to be confused with the {{focusScope item with which we want to regain focus but in fact its parent... it seems as though the example focusScope shouldn't have had its focus property set to false when fileMenu2 appears as it was probably enough to just set the focus of its parent i.e. the contentItem temporarily whilst the overlay was visible such that it would regain activeFocus when the fix for QTBUG-53275 kicks in.
Attachments
Issue Links
- relates to
-
QTBUG-53275 Popups should restore focus to the contentItem of ApplicationWindow upon closing
- Closed