Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Keyboard focus after popup Menu



  • I am working on a Qt Quick app that uses QtQuick.Controls Menu. I am having a problem with keyboard focus after the popup Menu is dismissed. Here is a simple example:

    import QtQuick
    import QtQuick.Controls
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Rectangle {
            anchors.centerIn: parent
            color: "lightsteelblue"; width: 240; height: 25
            Text {
                id: myText
                text:"Press A, B or C"
                anchors.centerIn: parent
            }
            Item {
                objectName: "Key Handler Item"
                id: keyHandler
                focus: true
                Keys.onPressed: (event)=> {
                    if (event.key === Qt.Key_A)
                        myText.text = 'Key A was pressed'
                    else if (event.key === Qt.Key_B)
                        myText.text = 'Key B was pressed'
                    else if (event.key === Qt.Key_C)
                        myText.text = 'Key C was pressed'
                }
            }
        }
    
    
        Menu {
            id: contextMenu
            MenuItem { text: "Menu item 1" }
            MenuItem { text: "Menu item 2" }
            MenuItem { text: "Another item" }
        }
    
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.RightButton
            onClicked: function(mouse) {
                console.log("Right-clicked")
                contextMenu.popup()
            }
        }
    
        Text {
            text: "Right-click for Menu"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 30
        }
    
        onActiveFocusItemChanged:  console.log("main: focus now on: ", activeFocusItem)
    }
    
    

    The output is:

    qml: main: focus now on:  QQuickItem(0x555555740010, "Key Handler Item")
    qml: Right-clicked
    qml: main: focus now on:  QQuickPopupItem(0x555555a03180)
    qml: main: focus now on:  MenuItem_QMLTYPE_2(0x555555a0e8c0)
    qml: main: focus now on:  MenuItem_QMLTYPE_2(0x555555a10f00)
    qml: main: focus now on:  QQuickRootItem(0x55555578d650)
    

    The focus goes to a QQuickRootItem. My question is, what can I do to ensure the keyboard focus goes back to the original item after the menu is dismissed?



  • For the record, I have figured it out. The Window has to be changed to ApplicationWindow, then the popup behaves as expected. The ApplicationWindow's contentItem acts as a FocusScope, so when the popup closes, it goes back to whatever had the focus before. If the action triggered by the menu changes the focus, that's ok too.

    There is still something weird happening in my application, which was already using ApplicationWindow. For some reason the activeFocus was on the QQuickRootItem, not on the QQuickContentItem. I have added this code and it fixes it:

    onActiveFocusItemChanged: {
         if (activeFocusItem === contentItem.parent)
             contentItem.focus = true
    }
    

Log in to reply