Can't change properties of ListElement with setProperty methode
-
Hello everyone,
I was working on my drawer menu for an andriod app and filled the menupoint with using ListView. I want to hightlight the chosen ListElement permanently, until the user goes to the menu again and choses to click another ListElement. In that case the old highlighted ListElement should go back to orignal color and the newly clicked on to the highlighted color.
The problem is that I cannot change the properties of non-current index ListElements.
Let's say I have a basiccode like:
ListModel{ id:model ListElement{itemcolor:"red"} ListElement{itemcolor:"red"} //Listview and other code... MouseArea { anchors.fill: parent onClicked: { model.currentIndex = index model.setProperty(1,"itemcolor","blue") } }
So in this shorten code to illustrate my problem, when I click the second ListElement, I would expect the first one to go blue, but it instead gives me this error in the console:
TypeError: Property 'setProperty' of object QQmlDMAbstractItemModelData(0x78c9dbf180) is not a function
However I even checked in the documentation of ListModel QML Type (https://doc.qt.io/qt-6/qml-qtqml-models-listmodel.html) and see there, setProperty function is defined in the same way as I use it. So why can't I change the properties of other ListElements by clicking one specific Listelement?
I'm using Qt6.5.
Thanks in advance for your support.
-
@Luffy97 that's because you are in a delegate and
model
here refers to a context property exposing data for the current row. It shadows the id of your ListModel. Use another id and you should be fine.Note that I wouldn't store the selected state in the model. Doing it in the view is easier usually.
-
-
This post is deleted!