Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Nested QML Menu from nested QStandardItemModel



  • 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)]


Log in to reply