[SailfishDevel] Dynamically populate a ContextMenu

Martin Kampas martin.kampas at tieto.com
Mon Nov 25 08:25:19 UTC 2013


Hi,

simply assign id to one of the static items, e.g. firstStatic, and then use 
firstStatic.parent instead of menu._contentColumn as parent for the 
dynamic items -- this way you do not rely on implementation details:

menu: ContextMenu {
    id: menu
    MenuItem { id: firstStatic; text: "Static 1" }
    MenuItem { text: "Static 2" }
}

Component.onCompleted: {
    console.debug("Populating menu")
    for (var i=0; i< 4 ; i++){
        var newMenuItem = menuItemComp.createObject(firstStatic.parent, 
                {"text" : "Dynamic" + i})
    }
}

Event better consider using Repeater and provide the list of dynamic 
items as item model[1] (assiging a number as a model gives exactly the 
same result as in your original example):

ApplicationWindow {
    initialPage: Page {
        ComboBox {
            width: parent.width
            label: "Menu"

            menu: ContextMenu {
                id: menu
                MenuItem { text: "Static 1" }
                MenuItem { text: "Static 2" }
                Repeater {
                        model: 4
                        MenuItem { text: "Dynamic " + index }
                }
            }
        }
    }
}


BR,
Martin

[1] https://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-repeater.html#model-prop


On Saturday, November 23, 2013 04:06:40 PM 
christopher.lamb at thurweb.ch wrote:
> Hi Andrey
> 
> Zitat von "Andrey Kozhevnikov" <coderusinbox at gmail.com>:
> > you may need to make childs of menu._contentColumn, not just menu.
> 
> I thought that the underscore stuff are private internal properties /
> functions that we mere mortals should not be using ....8-) (Not that
> that would necessarily stop me).
> 
> Chris
> 
> 
> _______________________________________________
> SailfishOS.org Devel mailing list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20131125/020798f6/attachment.html>


More information about the Devel mailing list