【问题标题】:QML - Filling menu with MenuItems from modelQML - 使用模型中的 MenuItems 填充菜单
【发布时间】:2023-02-10 00:03:09
【问题描述】:

我需要用模型中的 MenuItems 填充 QML 菜单

我发现我可以这样做:

Menu {
    id: contextMenu

    Instantiator {
       model: menuItems
       MenuItem {
          text: model.text
       }

       // The trick is on those two lines
       onObjectAdded: contextMenu.insertItem(index, object)
       onObjectRemoved: contextMenu.removeItem(object)
   }
}

此答案中描述的内容:

QML - Filling menu with model items

它现在部分工作,但我得到一个错误:

Parameter "object" is not declared

而且我不明白我应该将哪个对象传递给函数 contextMenu.insertItem(index, object)

【问题讨论】:

    标签: qt qml qtquickcontrols2


    【解决方案1】:

    您需要传入信号中的参数。阅读Signal parameters

    ListModel {
        id: menuItems
        ListElement { text:"hello1" }
        ListElement { text:"hello2" }
        ListElement { text:"hello3" }
    }
    
    Menu {
        id: contextMenu
        visible: true
    
        Instantiator {
           model: menuItems
    
           MenuItem {
              text: model.text
           }
    
           // The trick is on those two lines
           onObjectAdded: function(index, object) {
               contextMenu.insertItem(index, object)
           }
           onObjectRemoved: function(index, object) {
               contextMenu.removeItem(object)
           }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多