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. How can a GridView Element determine its index?
Forum Updated to NodeBB v4.3 + New Features

How can a GridView Element determine its index?

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 3.2k Views 1 Watching
  • 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.
  • U Offline
    U Offline
    uroller
    wrote on 24 Mar 2014, 21:31 last edited by
    #1

    I need elements to determine if they are first or last. I can get GridView.view.count in the delegate but I can not determine what the element's index is in the list. I am on Qt 4.7.

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on 24 Mar 2014, 22:43 last edited by
      #2

      you can maybe use a simple comparison if there is no attached index property?
      @
      var first = grid.children[0] === delegate
      @
      grid is the GridView id and delegate the id of the delegate ( i don't think you can use "this" :P )
      you might need to use grid.contentItem.children instead of grid.children i am not sure but many view container items in QML put the children in the contentItem and not the view directly (just loop though it or print the length).

      anyway my knowledge is that all view containers have an attached "index" property? I didn't try it with the GridView though.

      you may also try the int indexAt(int x, int y) function of the GridView, providing it with the position of the current delegate, but I don't know how well that works.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        uroller
        wrote on 26 Mar 2014, 13:25 last edited by
        #3

        I was using mapToItem() and indexAt() which seems pretty inefficient.

        @
        var o = mapToItem(GridView.view, 0, 0);
        var i = GridView.view.indexAt(o.x, o.y)
        @

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on 26 Mar 2014, 13:32 last edited by
          #4

          Ok to confirm this I just created a test project and GriedView works very well with the index property, so I don't know why it isn't working for you??

          My complete main.qml
          @
          import QtQuick 2.0

          GridView {
          width: 360
          height: 360
          model: 8
          delegate: Text { text: index }
          }
          @

          maybe you always tried to access the index with the GriedView property? it is just "index" or in my example "modelData" to access the item of the model (with an int model it is the same as index).

          B 1 Reply Last reply 2 Oct 2020, 21:00
          1
          • U Offline
            U Offline
            uroller
            wrote on 26 Mar 2014, 13:49 last edited by
            #5

            This seems to work:

            @
            var istop = GridView.view.children[0].children[0] != me;
            var isbottom = GridView.view.children[0].children[GridView.view.count -1] != me;
            @

            I'm not clear why I need the children[0].children[] but at least it seems to work.

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on 26 Mar 2014, 14:02 last edited by
              #6

              did you check my last post? also I did tell you before why you need to get children[0] or better contentItem. that is a design of the QML views they don't hold their delegate children directly, but in the contentItem child!

              so with my last example above if you print this:
              @
              Component.onCompleted: console.log(children.length, contentItem.children.length)
              @
              the output is "1 9", so the 9 children are in the contentItem! but you should be able to just use index instead of contentItem.children[0] etc.?

              1 Reply Last reply
              0
              • U Offline
                U Offline
                uroller
                wrote on 26 Mar 2014, 15:04 last edited by
                #7

                This what I ended up with:

                @
                var istop = return GridView.view.contentItem.children[0] != me;
                var isbottom = return GridView.view.contentItem.children[GridView.view.count -1] != me
                @

                me is the id of my component. This and delegate didn't work.

                Thanks for the help.

                1 Reply Last reply
                0
                • X Xander84
                  26 Mar 2014, 13:32

                  Ok to confirm this I just created a test project and GriedView works very well with the index property, so I don't know why it isn't working for you??

                  My complete main.qml
                  @
                  import QtQuick 2.0

                  GridView {
                  width: 360
                  height: 360
                  model: 8
                  delegate: Text { text: index }
                  }
                  @

                  maybe you always tried to access the index with the GriedView property? it is just "index" or in my example "modelData" to access the item of the model (with an int model it is the same as index).

                  B Offline
                  B Offline
                  brai
                  wrote on 2 Oct 2020, 21:00 last edited by
                  #8

                  @Xander84 said in How can a GridView Element determine its index?:

                  Ok to confirm this I just created a test project and GriedView works very well with the index property, so I don't know why it isn't working for you??

                  My complete main.qml
                  @
                  import QtQuick 2.0

                  GridView {
                  width: 360
                  height: 360
                  model: 8
                  delegate: Text { text: index }
                  }
                  @

                  maybe you always tried to access the index with the GriedView property? it is just "index" or in my example "modelData" to access the item of the model (with an int model it is the same as index).

                  This worked great for me.

                  I had to do something like the following to get the index to nested components. Hope this helps someone.

                  GridView {
                      width: 360
                      height: 360
                      model: 8
                      delegate: Rectanlge {
                          id: rect
                          property int itemIndex: index
                          Text {
                              text: rect.itemIndex
                          }
                      }
                  }
                  
                  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