Using a Repeater inside a ListModel to generate ListElements
Solved
QML and Qt Quick
-
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 elementsThank you!
-
This is not possible.
You can use your
dataList
context property directly as a model of aListView
instead.