Nested Menu with Instantiator
-
Hello all!
Is it possible to create context Menu with nested Menu's based on models with Instantiators? I have my own model of items (with the appropriate fields (name and value), each model has it's own model (with the different type).
I have tried the following code (and some slightly different versions), but it displays everything in the one menu, without nesting.
Menu { id: _menuDevices cascade: true Instantiator { id: _instantiator model: backend.getDevicesModel().getObjectsModel() delegate: MenuItem { width: _menuDevices.width, height: 50 text: model.name property QtObject device: model.pointer Menu { id: _menuParameters cascade: true Instantiator { id: _instantiatorInner model: device.getDeviceParametersModel().getObjectsModel() delegate: MenuItem { width: _menuParameters.width, height: 50 text: model.name } onObjectAdded: _menuParameters.insertItem(index, object) onObjectRemoved: _menuParameters.removeItem(object) } } } onObjectAdded: _menuDevices.insertItem(index, object) onObjectRemoved: _menuDevices.removeItem(object) } }
But in this approach I can see only the first level menu without nested ones. Looks like it happens cause inner Menus aren't direct children of the upper Menu's.
So question is: can I do what I've wanted? (and how if it's possible?)
-
Here I'm going to continue my classical "question-answer approach". After some time of research, I have found some mistakes in the code: upper level
MenuItem
s should be replaced withMenu
s and signals-slots should be redefined as follows:onObjectAdded: _menuDevices.insertMenu(index, object) onObjectRemoved: _menuDevices.removeMenu(object)
insertMenu
andremoveMenu
should be used instead ofinsertItem
andremoveItem
accordingly. It's very obvious thing, but it took some time to understand it in the context of the issue.