Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Extending a menu

Extending a menu

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 312 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zhavok
    wrote on last edited by Zhavok
    #1

    I am trying to add an item to a menu in QML. I have copied the example in the Docs (https://doc.qt.io/qt-6/qml-qtquick-controls-menu.html) under "Using Dynamic Object Creation" and still always get the error:

    "Could not convert argument 0 at"
    "expression for onClicked@qrc:/QtTemplate/Gui/gui/Main.qml:39"
    qrc:/QtTemplate/Gui/gui/Main.qml:39: TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed

    MyCode:

    import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    import QtTemplate.Gui as QtTemplate
    
    ApplicationWindow {
        id: root
        visible: true
        width: 500
        height: 500
        title: "QtTemplate"
    
        Row {
            anchors.centerIn: parent
    
            Component {
                id: menuItemComponent
    
                MenuItem {
                }
            }
    
            Button {
                id: button
                text: "Menu"
                onClicked: menu.open()
                Menu {
                    id: menu
                }
            }
    
            Button {
                text: "Add item"
                onClicked: {
                    let menuItem = menuItemComponent.createObject(menu.contentItem, {
                            text: qsTr("New item")
                        });
                    menu.addMenu(menuItem);
                }
            }
        }
    }
    

    Btw. I use Qt 6.6.0. Does anyone have a solution to the problem?

    B 1 Reply Last reply
    0
    • Z Zhavok

      I am trying to add an item to a menu in QML. I have copied the example in the Docs (https://doc.qt.io/qt-6/qml-qtquick-controls-menu.html) under "Using Dynamic Object Creation" and still always get the error:

      "Could not convert argument 0 at"
      "expression for onClicked@qrc:/QtTemplate/Gui/gui/Main.qml:39"
      qrc:/QtTemplate/Gui/gui/Main.qml:39: TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed

      MyCode:

      import QtQuick
      import QtQuick.Controls
      import QtQuick.Layouts
      import QtTemplate.Gui as QtTemplate
      
      ApplicationWindow {
          id: root
          visible: true
          width: 500
          height: 500
          title: "QtTemplate"
      
          Row {
              anchors.centerIn: parent
      
              Component {
                  id: menuItemComponent
      
                  MenuItem {
                  }
              }
      
              Button {
                  id: button
                  text: "Menu"
                  onClicked: menu.open()
                  Menu {
                      id: menu
                  }
              }
      
              Button {
                  text: "Add item"
                  onClicked: {
                      let menuItem = menuItemComponent.createObject(menu.contentItem, {
                              text: qsTr("New item")
                          });
                      menu.addMenu(menuItem);
                  }
              }
          }
      }
      

      Btw. I use Qt 6.6.0. Does anyone have a solution to the problem?

      B Offline
      B Offline
      Bandler
      wrote on last edited by
      #2

      @Zhavok The QML example is very strange and contains several errors.

      You can change the onClicked function like this to make it work:

      Button {
                  text: "Add item"
                  onClicked: {
                      let menuItem = menuItemComponent.createObject(menu.contentItem, {
                                                                        text: qsTr("New item")
                                                                    });                
                  }
      }
      
      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        Zhavok
        wrote on last edited by
        #3

        Ok thanks. That was easy XD

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved