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. Nested Menu with Instantiator
Qt 6.11 is out! See what's new in the release blog

Nested Menu with Instantiator

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 871 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.
  • S Offline
    S Offline
    St.Stanislav
    wrote on last edited by
    #1

    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?)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      St.Stanislav
      wrote on last edited by
      #2

      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 MenuItems should be replaced with Menus and signals-slots should be redefined as follows:

      	   onObjectAdded: _menuDevices.insertMenu(index, object)
      	   onObjectRemoved: _menuDevices.removeMenu(object)
      

      insertMenu and removeMenu should be used instead of insertItem and removeItem accordingly. It's very obvious thing, but it took some time to understand it in the context of the issue.

      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