Firstly, contrary to your title, you can make a QML Repeater work with a variable. The question wasn't clear on exactly what you did try, but, your use case indicates an array.
I present 3 ways of declaring a property that you can use in a Repeater:
property var fruitArrayQt5: ["apples", "oranges", "pears"]
property list<string> fruitArrayQt6: ["apples", "oranges", "pears"]
ListModel {
id: fruitModel
ListElement { name: "apples" }
ListElement { name: "oranges" }
ListElement { name: "pears" }
}
In the case of fruitArrayQt5 the property is declared as a var. Which, unfortunately, means subsequent changes to the array (e.g. via push/remove/etc) will not be signaled and the Repeater will not react to changes. The workaround is you have to keep reassigning the Repeater's model.
In the case of fruitArrayQt6 the property is declared as a list<string>. Because it is using the new Qt6 list primitive, it means subsequent changes to the array (e.g. via push/remove) WILL be signaled to the Repeater. It requires Qt6, possibly Qt6.4 to use this.
In the case of fruitModel changes done to the ListModel (e.g. via append/remove) WILL also be signal to the Repeater.
In the following example, we populate three Repeaters demonstrating the 3 types above. When you click on the Add button you see that the Repeaters only reacts to changes done to fruitArrayQt6 and fruitModel. The Repeater attached to fruitArrayQt5 doesn't react to changes. You have to enable the Workaround check box to force an update to Repeater's model property:
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
property var fruitArrayQt5: ["apples", "oranges", "pears"]
property list<string> fruitArrayQt6: ["apples", "oranges", "pears"]
ListModel {
id: fruitModel
ListElement { name: "apples" }
ListElement { name: "oranges" }
ListElement { name: "pears" }
}
RowLayout {
width: parent.width
ColumnLayout {
Layout.alignment: Qt.AlignTop
Repeater {
model: fruitModel
Text { text: "fruitModel: " + name }
}
}
ColumnLayout {
Layout.alignment: Qt.AlignTop
Repeater {
id: arrayRepeater
model: fruitArrayQt5
Text { text: "fruitArrayQt5: " + modelData }
}
}
ColumnLayout {
Layout.alignment: Qt.AlignTop
Repeater {
model: fruitArrayQt6
Text { text: "fruitArrayQt6: " + modelData }
}
}
}
footer: Frame {
RowLayout {
CheckBox {
id: workaround
text: qsTr("Workaround")
}
Button {
text: qsTr("Add")
onClicked: {
fruitModel.append({name:"bananas"});
fruitArrayQt5.push("bananas");
fruitArrayQt6.push("bananas");
if (workaround.checked)
arrayRepeater.model = fruitArrayQt5;
}
}
Button {
text: qsTr("Reset")
onClicked: {
while (fruitModel.count > 3)
fruitModel.remove(fruitModel.count - 1);
while (fruitArrayQt5.length > 3)
fruitArrayQt5.pop();
while (fruitArrayQt6.length > 3)
fruitArrayQt6.pop();
if (workaround.checked)
arrayRepeater.model = fruitArrayQt5;
}
}
}
}
}
You can Try it Online!