Using Repeater inside ListModel
-
Hello, I am trying something seemingly simple. I need a list of number with 3 distinct parts:
[ 15, 16, 17, 18, 18.5, 19, 19.5, 20, 20.5, 21, 21.5, 22, 22.5, 23, 24, 25, 26, 27, 28, 29, 30]As you can see, the first part increases by a rate of 1, then the second part by a rate of 0.5, and the last by a rate of 1 again. I know in advance the first numbers of each part (15, 18, 23) and the end of range (30). 21 elements in total.
I chose
ListModel
because I want to use the list in aListView
object. So I tried something like:ListModel { Repeater { model: beginPartTwo - beginPartOne ListElement { val: beginPartOne + index } // 15 16 17 } Repeater { model: (beginPartThree - beginPartTwo) * 2 ListElement { val: beginPartTwo + index * 0.5 } // 18 18.5 19 19.5 20 20.5 21 21.5 22 22.5 } Repeater { model: endOfRange - beginPartThree + 1 ListElement { val: beginPartThree + index } // 23 24 25 26 27 28 29 30 } }
But I get this error:
ListElement: cannot contain nested elements
Can someone recommend another way to automate the creation of this list?
-
https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method
Use the function calls to do this. -
Thank you! I wasn't aware of that function, it works now.
I'll leave the code if ever it can be useful for someone else.Component.onCompleted: { var i for(i = beginPartOne; i < beginPartTwo; i++) listModel.append({"val": i}) for(i = beginPartTwo; i < beginPartThree; i += 0.5) listModel.append({"val": i}) for(i = beginPartThree; i <= endOfRange; i++) listModel.append({"val": i}) } // each loop adds each segment below // 15 16 17 | 18 18.5 19 19.5 20 20.5 21 21.5 22 22.5 | 23 24 25 26 27 28 29 30