代码之家  ›  专栏  ›  技术社区  ›  Jack

Assign MenuItem.Shortcut指定的附加对象无效

  •  0
  • Jack  · 技术社区  · 6 年前

    以下代码:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.5
    import QtQuick.Controls.Styles 1.4
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("This is my application title!")
    
        ColumnLayout
        {
    
            id: col1
            spacing: 2
      
            MenuBar
            {
                Menu {
                    title: "File"
                    MenuItem {
                        text: "Open"
                        Shortcut: "Ctrl+O"
                        onTriggered: console.log("Ctrl+O trigged")
                    }
                    MenuItem { text: "Paste link from Ctrl+V" }
                    MenuItem { text: "Save log as" }
                }
                Menu {  title: "Help" }
                Menu {  title: "About" }
                Menu {  title: "Exit" }
            }
       }
    

    给出以下错误:

    qrc:/main。qml:25:21:附加对象分配无效

    线路接通错误为 Shortcut: "Ctrl+O" . Qt documentation 举个这样的例子。我错过了什么?

    编辑:添加了文档链接。 编辑2:更新的导入

    0 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc    6 年前

    在qml中,至少有两组控件:

    • Qt快速控制2

    这些组中的组件与导致错误的组件相同,因为您试图将MenuItem的属性从一个组应用到另一个组(请检查导入以了解错误)。

    import QtQuick 2.12
    
    import QtQuick.Controls 1.4
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("This is my application title!")
    
        menuBar: MenuBar{
            Menu {
                title: "File"
                MenuItem {
                    text: "Open"
                    shortcut: "Ctrl+O"
                    onTriggered: console.log("Ctrl+O trigged")
                }
                MenuItem{ text: "Paste link from Ctrl+V" }
                MenuItem { text: "Save log as" }
            }
            Menu {  title: "Help" }
            Menu {  title: "About" }
            Menu {  title: "Exit" }
        }
    }
    

    Qt快速控制2

    import QtQuick 2.12
    
    import QtQuick.Controls 2.12
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("This is my application title!")
    
        menuBar: MenuBar{
            Menu {
                title: "File"
                Action {
                    text: "Open"
                    shortcut: "Ctrl+O"
                    onTriggered: console.log("Ctrl+O trigged")
                }
                Action { text: "Paste link from Ctrl+V" }
                Action { text: "Save log as" }
            }
            Menu {  title: "Help" }
            Menu {  title: "About" }
            Menu {  title: "Exit" }
        }
    }
    

    可能你会有一个类似的风格问题,所以建议你阅读 this answer 在这里,我指出,如果您希望组合两个模块的组件,那么使用名称空间是一种解决方案。

    注: QML是区分大小写的,在您指定的文档中 s 但是你用 S 霍特库特。