Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Discover and share your #QtStories


    Fill out our CSRD Survey


    Solved Nested Menu with Instantiator

    QML and Qt Quick
    1
    2
    192
    Loading More Posts
    • 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
      St.Stanislav last edited by

      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 Reply Quote 0
      • S
        St.Stanislav last edited by

        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 Reply Quote 0
        • S
          St.Stanislav last edited by

          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 Reply Quote 0
          • First post
            Last post