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

Using a Repeater inside a ListModel to generate ListElements



  • I am trying to use a backend C++ model inside my ListView, and was trying to create ListElements with a Repeater.

    Here's the ListModel code I have:

    import QtQuick 2.0
    
    ListModel {
    //    ListElement {
    //        name: "Forceps"
    //        weight: 1
    //    }
    
    //    ListElement {
    //        name: "Endoscope"
    //        weight: 2
    //    }
    
    //    ListElement {
    //        name: "Drill"
    //        weight: 3
    //    }
    
        Repeater {
            model: myModel
    
            ListElement {
                name: myModel.toolName
                weight: myModel.toolWeight
            }
        }
    }
    

    Here is the relevant code from main.cpp

        QList<QObject*> dataList;
    
        dataList.append(new ToolViewModel(0, "Forceps"));
        dataList.append(new ToolViewModel(1, "Endoscope"));
        dataList.append(new ToolViewModel(2, "Drill"));
        QQuickView view;
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
    

    I wanted to know if there was any way this could work, i.e using a Repeater inside a ListModel. Currently getting this error:
    ListElement: cannot contain nested elements

    Thank you!


  • Qt Champions 2018

    This is not possible.

    You can use your dataList context property directly as a model of a ListView instead.


  • Qt Champions 2018

    This is not possible.

    You can use your dataList context property directly as a model of a ListView instead.


Log in to reply