1
votes

I want to dynamically build a QML context menu. When I call 'addMenu' the menu entry is added, but I get this warning:

QQmlComponent: Created graphical object was not placed in the graphics scene.

Here is the code to reproduce the issue:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480

    Menu {
        id:contextMenu
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        onClicked: {
            contextMenu.addMenu("NewMenu");
            contextMenu.popup();
        }
    }
}

What am I doing wrong here?

2
It works in 5.5 witout warning so maybe you just need to update. - folibis
like said @folibis no warning on 5.5, try this maybe it will work better contextMenu.insertMenu(contextMenu.__currentIndex,"NewMenu") - Mido
Thanks @folibis, I'll do the update. - fscibe

2 Answers

1
votes

This looks like a bug in Qt to me. If you look at Menu.qml (where the Menu QML component is defined), addMenu is defined as follows:

function addMenu(title) {
    return root.insertMenu(items.length, title)
}

function insertMenu(index, title) {
    if (!__selfComponent)
        __selfComponent = Qt.createComponent("Menu.qml", root)
    var submenu = __selfComponent.createObject(__selfComponent, { "title": title })
    root.insertItem(index, submenu)
    return submenu
}

/*! \internal */
property Component __selfComponent: null

The important line here is __selfComponent.createObject(__selfComponent, { "title": title }). This sets __selfComponent (the Menu component, not the menu itself) as the parent for the newly-created sub menu. I believe that this is wrong, the parent should instead be set to root, the menu itself.

0
votes

Add visible: true to ApplicationWindow like that:

ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true
    Menu {
        id:contextMenu
    }
...

Maybe that helps.