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. ListView inside Menu
Forum Updated to NodeBB v4.3 + New Features

ListView inside Menu

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 434 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.
  • A Offline
    A Offline
    Archie888
    wrote on last edited by
    #1

    In the doc it says:

    contentModel : model

    This property holds the model used to display menu items.

    The content model is provided for visualization purposes. It can be assigned as a model to a content item that presents the contents of the menu.

    Menu {
        id: menu
        contentItem: ListView {
            model: menu.contentModel
        }
    }
    

    The model allows menu items to be statically declared as children of the menu.

    (Source: https://doc.qt.io/qt-6/qml-qtquick-controls2-menu.html#contentModel-prop )

    --> Are there any working examples of doing this? I can't get it working in my tests.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Archie888
      wrote on last edited by Archie888
      #3

      I finally got it rendering, I used anchors.fill: parent on the ListView, which caused nothing to be rendered.

      This will render the same as when the ListView is not used:

          Menu {
              id: testMenu
              width: 150;
              height: 150
      
              MenuItem {
                  text: "New..."
              }
              MenuItem {
                  text: "Open..."
              }
              MenuItem {
                  text: "Save"
              }
      
              contentItem: ListView {
                  model: testMenu.contentModel
              }
          }
      

      Why would I want to do that? I assume that the point is to use a delegate in the ListView to render imaginary menu elements, no? But adding the delegate, nothing is rendered:

          Menu {
              id: testMenu
              width: 150;
              height: 150
      
              MenuItem {
                  text: "New..."
              }
              MenuItem {
                  text: "Open..."
              }
              MenuItem {
                  text: "Save"
              }
      
              contentItem: ListView {
                  model: testMenu.contentModel
                  delegate: Rectangle {
                      color: "blue"
                      width: 150
                      height: 30
                      Text { text: modelData.text }
                  }
              }
          }
      

      EDIT:

      Ok, added anchors.fill: parent to the delegate, and it started rendering. Now it renders exactly also like when the ListView is not used, and the delegate is not showing at all, it is just a normal menu:

      Menu {
              id: testMenu
              width: 150;
              height: 150
              title: qsTr("TestMenu Root")
      
              MenuItem {
                  text: "New..."
              }
              MenuItem {
                  text: "Open..."
              }
              MenuItem {
                  text: "Save"
              }
      
              contentItem: ListView {
                  model: testMenu.contentModel
                  delegate: Rectangle {
                      anchors.fill: parent
                      color: "blue"
                      width: 150
                      height: 30
                      Text { text: modelData.text }
                  }
              }
          }
      

      What is the point, and is there some use for this ListView as contentItem mechanic? The delegate seems not to be used.

      EDIT 2:

      Ok, so it looks like the ListView can be used to modify how the MenuItems flow and so on, by setting properties and so on. A horizontal menu can be achieved like this:

          Menu {
              id: testMenu
              width: 150;
              height: 150
              title: qsTr("TestMenu Root")
      
              MenuItem {
                  text: "New..."
              }
              MenuItem {
                  text: "Open..."
              }
              MenuItem {
                  text: "Save"
              }
      
              contentItem: ListView {
                  model: testMenu.contentModel
                  orientation: Qt.Horizontal
              }
          }
      

      However, the ListView delegate can't be used, and I am not sure how to customize MenuItem visuals in other ways without rolling out your own menus.

      EDIT 3:

      Ok, found how to do that, but mildly disappointed that the ListView delegate can't be used.

      The trick is to set up a custom MenuItem as the delegate of the Menu itself.

      https://doc.qt.io/qt-6/qtquickcontrols2-customize.html#customizing-menu

      Anyways, I hope this thread helps someone who is wondering about the same thing.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #2

        What is not working ? Minimal example would help. You can create few menuitems and assign the listView as content item. Menu will be like a listView style with menuitem as added.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Archie888
          wrote on last edited by Archie888
          #3

          I finally got it rendering, I used anchors.fill: parent on the ListView, which caused nothing to be rendered.

          This will render the same as when the ListView is not used:

              Menu {
                  id: testMenu
                  width: 150;
                  height: 150
          
                  MenuItem {
                      text: "New..."
                  }
                  MenuItem {
                      text: "Open..."
                  }
                  MenuItem {
                      text: "Save"
                  }
          
                  contentItem: ListView {
                      model: testMenu.contentModel
                  }
              }
          

          Why would I want to do that? I assume that the point is to use a delegate in the ListView to render imaginary menu elements, no? But adding the delegate, nothing is rendered:

              Menu {
                  id: testMenu
                  width: 150;
                  height: 150
          
                  MenuItem {
                      text: "New..."
                  }
                  MenuItem {
                      text: "Open..."
                  }
                  MenuItem {
                      text: "Save"
                  }
          
                  contentItem: ListView {
                      model: testMenu.contentModel
                      delegate: Rectangle {
                          color: "blue"
                          width: 150
                          height: 30
                          Text { text: modelData.text }
                      }
                  }
              }
          

          EDIT:

          Ok, added anchors.fill: parent to the delegate, and it started rendering. Now it renders exactly also like when the ListView is not used, and the delegate is not showing at all, it is just a normal menu:

          Menu {
                  id: testMenu
                  width: 150;
                  height: 150
                  title: qsTr("TestMenu Root")
          
                  MenuItem {
                      text: "New..."
                  }
                  MenuItem {
                      text: "Open..."
                  }
                  MenuItem {
                      text: "Save"
                  }
          
                  contentItem: ListView {
                      model: testMenu.contentModel
                      delegate: Rectangle {
                          anchors.fill: parent
                          color: "blue"
                          width: 150
                          height: 30
                          Text { text: modelData.text }
                      }
                  }
              }
          

          What is the point, and is there some use for this ListView as contentItem mechanic? The delegate seems not to be used.

          EDIT 2:

          Ok, so it looks like the ListView can be used to modify how the MenuItems flow and so on, by setting properties and so on. A horizontal menu can be achieved like this:

              Menu {
                  id: testMenu
                  width: 150;
                  height: 150
                  title: qsTr("TestMenu Root")
          
                  MenuItem {
                      text: "New..."
                  }
                  MenuItem {
                      text: "Open..."
                  }
                  MenuItem {
                      text: "Save"
                  }
          
                  contentItem: ListView {
                      model: testMenu.contentModel
                      orientation: Qt.Horizontal
                  }
              }
          

          However, the ListView delegate can't be used, and I am not sure how to customize MenuItem visuals in other ways without rolling out your own menus.

          EDIT 3:

          Ok, found how to do that, but mildly disappointed that the ListView delegate can't be used.

          The trick is to set up a custom MenuItem as the delegate of the Menu itself.

          https://doc.qt.io/qt-6/qtquickcontrols2-customize.html#customizing-menu

          Anyways, I hope this thread helps someone who is wondering about the same thing.

          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