首页 > 解决方案 > 无法通过快捷方式访问 QML 中的菜单项

问题描述

这是 QtQuick.Controls.12.2 的代码。它显示正确,但是当我按 Ctrl 时没有任何反应。

我希望打印语句能够执行。我在这里做错了什么?

    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"
                             }
                        }
                    }
        }
     }

标签: javascriptlinuxqtqml

解决方案


实际上,看起来不可能使用单个修饰符(Ctrl/Shift/...)作为快捷方式值。

见这个类似的问题

将工作:

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

不管用:

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

一个可能的解决方法是在菜单外捕捉按键:

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


    Menu {
        // ...
    }   
}

推荐阅读