简体   繁体   English

无法通过快捷方式访问 QML 中的菜单项

[英]Unable to access menu item in QML through shortcuts

This is the code with QtQuick.Controls.12.2.这是 QtQuick.Controls.12.2 的代码。 It displays properly but when I press Ctrl nothing happens.它显示正确,但是当我按 Ctrl 时没有任何反应。

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

Indeed it looks like it's not possible to use a single modifier (Ctrl/Shift/...) as a shortcut value.实际上,看起来不可能使用单个修饰符(Ctrl/Shift/...)作为快捷方式值。

( 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 {
        // ...
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM