In small example below, I'm trying to create rectangles whose width depends on foo. As we all know, we cannot use property binding in ListElement because ListElement doesn't allow to use script for property value.
import QtQuick 2.0
import QtQuick.Controls
Window {
width: 400
height: 300
visible: true
property int foo: 10
ListModel {
id: listModel
// ListElement {
// width: foo // error: ListElement: cannot use script for property value
// }
}
Row {
anchors.top: parent.top
Repeater {
id: repeater
model: listModel
delegate: Rectangle {
width: model.width
height: 50
color: "green"
}
}
}
Button {
text: "add item"
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
onClicked: {
listModel.append({
"width": foo // works, but is not a binding
})
}
}
}
In order to use script, I have to try to append in function. However, in this way, if foo is changed somewhere, the width of rectangles won't change accordingly. So anyone knows a better solution?
You can update a ListModel when your data has changed. You can do something like this:
It's definitely clunkier than a direct binding within the ListElement, but it works.