[solved, kind of] How to change the state of a specific delegate in a GridView?
-
I'm using a GridView in combination with a MouseArea for a chess board "here":https://gitorious.org/testdef/testdef/blobs/master/testdef/qml/OnlineBoard.qml (lines 162 and below). Changing the state of the current selected delegate is not a problem but what about changing the state of other delegates?
For example:
Tapping the delegate with index 15 (and empty square) sets the state "wrongTap". Fine.
Now tapping the delegate with index 0 (a white rook) sets the state "pieceSelected". Also fine but...
... leaving the delegate 15 as "wrongTap" at this point is dirty and actually creates bugs in certain combinations of taps. Ideally one should be able to set the state of index 15 back to ''.
I can't find the way to do this. Hopefully there is a way, though.
-
Not sure if this is what you want to do, but you can access an Item from your model (that the Grid uses) like this:
var localModel = modelInGrid
itemInGrid = localModel[index]where modelInGrid is the one that you've given to the GridView, index is some index in the model and itemInGris is the item at "index".
Not sure you asked for this - but hope it helps :)
-
I hope something like...
@var modelVar = myGrid.model
itemInGrid = modelVar[15]
itemInGrid.state = ""
@
...would work.But a I recall, this is a hack that may or may not work in future versions of QML. Unfortunately I can't recall a better way to access an item with some index in any given model that the GridView is using.
However, QML's own model's, namely ListModel and XmlListModel, provide methods to get the item at a given index. So if you use either of those two as model for your grid, you can directly access the model content with the get(int index) method.
http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html#get-method
http://doc.qt.nokia.com/4.7-snapshot/qml-xmllistmodel.html#get-methodFor example
@item = myGrid.model.get(15)
item.state = ""@But if you don't use either of those two model types, then the hack would work. It's a bit sad that QML does not provide a generic way to access a GridView's och ListView's items by index with any given model - or at least I haven't figured out how.
-
After trying further I decided to stop trying to change states of elements in a grid (other than the one for the 'current' index). As you say the solutions now are quite hackish.
Instead, as your recommended now I'm handling the events on that board applying all changes to the ListModel directly through get/set methods. Probably this is a better solution anyway...
Thanks for the lesson though! I did learn something and your answer pushed me away... to a better path. :)