Skip to content
Advertisement

Unable to access menu item in QML through shortcuts

This is the code with QtQuick.Controls.12.2. It displays properly but when I press Ctrl nothing happens.

I expect the print statement to execute. What am I doing wrong here?

    Menu
    {
        title: qsTr("File")

        MenuItem
        {
            id: new_
            text: "qqq"

            onTriggered:
            {
                console.log("saasd")
            }

            action:
                 Action
                 {
                     shortcut: "Ctrl"
                     onTriggered: console.log("sad0asd")

                 }

            contentItem:
                    Row
                    {
                        spacing: 70
                        Text
                        {
                            text: new_.text
                            font: menuItem.font
                            opacity: enabled ? 1.0 : 0.3
                            color: menuItem.highlighted ? "#ffffff" : "#21be2b"
                        }

                        Row
                        {
                            spacing: 5
                            Rectangle
                            {
                                color: "blue"; height: decoration.getHeight(15); width: height
                            }

                            Text
                            {
                                text: "Ctrl"
                                font: menuItem.font
                                opacity: enabled ? 1.0 : 0.3
                                color: new_.highlighted ? "#ffffff" : "#21be2b"
                             }
                        }
                    }
        }
     }

Advertisement

Answer

Indeed it looks like it’s not possible to use a single modifier (Ctrl/Shift/…) as a shortcut value.

(See this similar question)

Will work:

shortcut: "Ctrl+K" // modifier + key
shortcut: "K" // unique key

Will not work:

shortcut: "Ctrl"
shortcut: "Shift"
...

A possible workaround for you is to catch the key press outside of the menu:

Item {
    //...
    
    focus: true
    Keys.onPressed: {
        if (event.key === Qt.Key_Control) {
           console.log("sad0asd")
        }
    }


    Menu {
        // ...
    }   
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement