Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Nested QML Menu from nested QStandardItemModel
Qt 6.12 LTS is out! See what's new in the release blog

Nested QML Menu from nested QStandardItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 423 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    PavloPonomarov
    wrote on last edited by PavloPonomarov
    #1

    Hello. I have a nested model subclassed from QStandardItemModel and filled like this:

    /*----MyModel.cpp----*/
    for(int row = 0; row < 10; row++){
      setData(index(row,0), "Item"+QString::number(row), Name);
      setData(index(row,0), 0, Value);
      setData(index(row,0), "description of item"+QString::number(row), Description);
      QStandardItem *item = itemFromIndex(index(row,0));
      for(int i = 0; i < 10; i++){
        QStandardItem *subitem = new QStandardItem;
        subitem->setData("SubItem"+QString::number(i), Name);
        subitem->setData(0, Value);
        subitem->setData("Description of subItem"+QString::number(i), Description);
        item->appendRow(subitem);
      }
    }
    

    So, to create a Menu from this model I use Instantiator:

    import MyModel 1.0 //it is registered in main.cpp
    
    MyModel{
       id:myModel
    }
    
    Menu{
      id: topMenu
      title: "Example"
      Instantiator{
         model: myModel
         onObjectRemoved: topMenu.removeItem( object )
         delegate: Component{
             Loader:{
                 id: loader
                 onStatusChanged: if (status == Loader.Ready) {
                                   topMenu.insertMenu( index, item );
                                   loader.item.itemModel = model;
                                  }
                  source: "SubMenuLoader.qml"
             }
         }
      }
    }
    

    My problem is that the model I pass to SubMenuLoader.qml is QQmlDMAbstractItemModelData , which means when I create the Instantiator in SubMenuLoader.qml I will only have the current item, there is no property or method to get children of this model item in QML. I also didn't manage to expose items children from C++, because QML won't understand QList<QStandardItem*>. Even after I declared it as metatype it turns into [QVariant(QStandardItem*, ), QVariant(QStandardItem*, ) ...(and so on)]

    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
    • Unsolved