Extending a menu
Unsolved
QML and Qt Quick
-
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 allowedMyCode:
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?
-
@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") }); } }