Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved, kind of] How to change the state of a specific delegate in a GridView?
QtWS25 Last Chance

[solved, kind of] How to change the state of a specific delegate in a GridView?

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qgil
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kypeli
      wrote on last edited by
      #2

      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 :)

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qgil
        wrote on last edited by
        #3

        Thanks Kypeli but...

        Following your syntax, how would you set the state of the item with index 15 to ''?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kypeli
          wrote on last edited by
          #4

          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-method

          For 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.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qgil
            wrote on last edited by
            #5

            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. :)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kypeli
              wrote on last edited by
              #6

              Good to read! Manipulating the data in the model and letting the view react to model changes is a lot better approach! :) Congratulations!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved