Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-83698

Using Keys.onReturnPressed from Button to open Menu causes the first MenuItem to get triggered on show

    XMLWordPrintable

Details

    • Windows
    • a5e1c787f6512013a092f7708c6928b5a9e263c2 (qt/qtquickcontrols2/dev) 1a3d27b017fae0471a82870b28e1e808bc2aac33 (qt/qtquickcontrols2/5.15) f8dd521288c9bd36c4a6a003c2b1cba94b1a3b19 (qt/qtquickcontrols2/5.12)

    Description

      We have code that opens a Menu when the user tabs onto a button and pressed Enter/Return.

      The code no longer works correctly in Qt 5.14, it opens the menu and then immediately triggers the first menu item. See small repro project MenuProblems.zip

      The relevant QML from that attachment:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import QtQuick.Controls 2.12
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Text {
              id: description
              width: 400
              text: "Repro: use keyboard navigation - TAB onto the Button. Press RETURN or ENTER (not SPACE) to activate the button. \n" +
                    "Menu will open and will also activate Command 1 without user pressing any key (BUG) \n" +
                    "Pressing SPACE or clicking the button works correctly. \n"
      
              wrapMode: Text.WordWrap
          }
      
          Button {
              id: menuButton
      
              anchors.top: description.bottom
              anchors.topMargin: 50
      
              text: "Open menu"
      
              onClicked: {
                  theMenu.open();
              }
      
              Keys.onReturnPressed: onClicked()
              Keys.onEnterPressed: onClicked()
          }
      
          Menu {
              id: theMenu
              parent: menuButton
      
              onOpened: {
                  // NOTE: forceActiveFocus is needed for to place the focus rectangle on the
                  // first MenuItem, which is standard behavior on Windows when using keys to navigate
                  theMenu.itemAt(0).forceActiveFocus();
              }
      
              MenuItem {
                  id: command1
                  text: "Command 1"
      
                  onTriggered: console.log(text + " triggered")
              }
      
              MenuItem {
                  id: command2
                  text: "Command 2"
                  onTriggered: console.log(text + " triggered")
              }
      
              MenuItem {
                  id: command3
                  text: "Command 3"
                  onTriggered: console.log(text + " triggered")
              }
          }
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            mitch_curtis Mitch Curtis
            cosminp Cosmin Pancratov
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes