<div dir="ltr">That's a very universal solution to a specific problem and one that's going to have all kinds of unintended consequences. Without knowing why focus was taken from an item and given to another and why that other object relinquished focus it's impossible to know whether focus should be restored to that first item.  That memory may have to go back more than one item as well, after all if you open one menu then another then the previous focus item is the first window and that's obviously not what you want to give focus to.<div><br></div><div>What you want to do instead is introduce a FocusScope to ApplicationWindow. If the window's content item is a focus scope then giving focus to a menu which is outside of this scope will not affect focus within the scope, then whatever logic closes the menu can give focus back to the content item and that will implicitly return active focus to your item in the window body.</div><div><br></div><div><br></div><div>Andrew</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 6, 2016 at 12:08 AM, Mitch Curtis <span dir="ltr"><<a href="mailto:mitch.curtis@qt.io" target="_blank">mitch.curtis@qt.io</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Consider the example below (requires Qt 5.7):<br>
<br>
    import QtQuick 2.6<br>
    import QtQuick.Layouts 1.1<br>
    import QtQuick.Window 2.2<br>
    import QtQuick.Controls 2.0<br>
<br>
    ApplicationWindow {<br>
        width: 400<br>
        height: 200<br>
        visible: true<br>
<br>
        onActiveFocusItemChanged: print("activeFocusItem", activeFocusItem)<br>
<br>
        header: ToolBar {<br>
            RowLayout {<br>
                focus: false<br>
                implicitWidth: children[0].implicitWidth<br>
                implicitHeight: children[0].implicitHeight<br>
<br>
                ToolButton {<br>
                    text: qsTr("File")<br>
                    onClicked: fileMenu.open()<br>
<br>
                    Menu {<br>
                        id: fileMenu<br>
                        y: parent.height<br>
<br>
                        MenuItem {<br>
                            text: qsTr("New")<br>
                        }<br>
                        MenuItem {<br>
                            text: qsTr("open")<br>
                        }<br>
                        MenuItem {<br>
                            text: qsTr("Close")<br>
                        }<br>
                    }<br>
                }<br>
            }<br>
        }<br>
<br>
        FocusScope {<br>
            id: focusScope<br>
            focus: true<br>
            anchors.fill: parent<br>
<br>
            property bool toggled: false<br>
            onToggledChanged: print("toggled", toggled)<br>
<br>
            Keys.onPressed: {<br>
                if (event.modifiers === Qt.AltModifier) {<br>
                    focusScope.toggled = true;<br>
                }<br>
            }<br>
            Keys.onReleased: {<br>
                if ((event.modifiers === Qt.AltModifier || event.key === Qt.Key_Alt) && !event.isAutoRepeat) {<br>
                    focusScope.toggled = false;<br>
                }<br>
            }<br>
<br>
            RowLayout {<br>
                anchors.centerIn: parent<br>
<br>
                Button {<br>
                    id: penButton<br>
                    text: qsTr("Pen")<br>
                    highlighted: !focusScope.toggled<br>
                }<br>
                Button {<br>
                    id: eyedropperButton<br>
                    text: qsTr("Eyedropper")<br>
                    highlighted: focusScope.toggled<br>
                }<br>
            }<br>
        }<br>
    }<br>
<br>
The idea is that holding the Alt key down will toggle between two "modes". The mode is indicated by a highlighted button.<br>
<br>
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). This is explained here [1]:<br>
<br>
"When a QML Item explicitly relinquishes focus (by setting its focus property to false while it has active focus), the system does not automatically select another type to receive focus. That is, it is possible for there to be no currently active focus."<br>
<br>
I'm not sure if "explicitly relinquishes focus" is exactly what's happening here, though. The FocusScope loses focus because something else was open temporarily. So now the developer has no other option than to listen to e.g. the menu visibility changes and explicitly set focus on the FocusScope accordingly. That's pretty gross if you ask me. I think we can do better. Specifically, I think that we should remember the last focused item, and give that focus, rather than give it to the root item. The question is, would it be an acceptable change for Qt 5? I'd assume that users are already working around this anyway, and so the fix wouldn't hurt.<br>
<br>
[1] <a href="http://doc.qt.io/qt-5/qtquick-input-focus.html" rel="noreferrer" target="_blank">http://doc.qt.io/qt-5/qtquick-input-focus.html</a><br>
_______________________________________________<br>
Development mailing list<br>
<a href="mailto:Development@qt-project.org">Development@qt-project.org</a><br>
<a href="http://lists.qt-project.org/mailman/listinfo/development" rel="noreferrer" target="_blank">http://lists.qt-project.org/mailman/listinfo/development</a><br>
</blockquote></div><br></div>