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. QtQuick 2 TableView row number
Forum Updated to NodeBB v4.3 + New Features

QtQuick 2 TableView row number

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.3k 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.
  • rincewindR Offline
    rincewindR Offline
    rincewind
    wrote on last edited by rincewind
    #1

    Hi

    I'm using a TableView and I want to get the row number of a delegate. I assumed the index would consist of a row and column, but it turns out it is a number. The index property does follow some pattern, but I can't find it documented anywhere.

    I have a table with 4 columns, and the index property contains the values as indicated in the table:

    0, 3000, 6000, 9000
    1, 3001, 6001, 9001
    2, 3002, 6002, 9002
    

    Can I assume the index values will always follow this pattern and map this to row and column numbers or is there some other way to determine the row number of a delegate in a TableView?

    1 Reply Last reply
    0
    • rincewindR Offline
      rincewindR Offline
      rincewind
      wrote on last edited by rincewind
      #4

      There are actually row and column properties that are exposed! I don't know how I missed this.

      Some digging before I realized this, to explain the integer index:
      So it seems Qt generates a unique index for each item based on the row count. The items in the first column are numbered 0...rowCount -1, the seconds column rowCount...rowCount + rowCount -1, etc.

      Browsing through the Qt code, it seems this mapping between a QModelIndex and the QML (int) index is maintained in QQmlAdaptorModel.

      @jay1 I don't need a persistent index, I just want to get to the current index.

      @6thC Yes, I have a QAbstractItemModel derived model.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jay1
        wrote on last edited by
        #2

        In TableView, the model index is exposed as an accessible index property.

        In QModelIndex, Each top-level item in a model is represented by a model index that does not have a parent index. Each top level item start with a zero index i.e. as 0, 3000, 6000, 9000 ..... but this is not persisted. If we want to persist the index, we need to use the QPeristentModelIndex, this provides a way to store the index by an application and can be accessible. Using C++ for assigning the indexes is an option to try.

        For reference:
        https://doc.qt.io/qt-5/qml-qtquick-tableview.html
        https://doc.qt.io/qt-5/qpersistentmodelindex.html

        1 Reply Last reply
        -1
        • 6thC6 Offline
          6thC6 Offline
          6thC
          wrote on last edited by
          #3

          QAbstractTableModel https://doc.qt.io/qt-5/qabstracttablemodel.html
          QSortFilterProxyModel https://doc.qt.io/qt-5/qsortfilterproxymodel.html

          For TableView I just bit the bullet and went the full model and haven't looked back. It might take a day or a week if you struggle. But once it's in your head it's really simple and works very well with QML and std:: containers. Or at least, that's been my experience.

          So my index just matches my std:: container data / index and that keeps things rather simple.

          1 Reply Last reply
          -1
          • rincewindR Offline
            rincewindR Offline
            rincewind
            wrote on last edited by rincewind
            #4

            There are actually row and column properties that are exposed! I don't know how I missed this.

            Some digging before I realized this, to explain the integer index:
            So it seems Qt generates a unique index for each item based on the row count. The items in the first column are numbered 0...rowCount -1, the seconds column rowCount...rowCount + rowCount -1, etc.

            Browsing through the Qt code, it seems this mapping between a QModelIndex and the QML (int) index is maintained in QQmlAdaptorModel.

            @jay1 I don't need a persistent index, I just want to get to the current index.

            @6thC Yes, I have a QAbstractItemModel derived model.

            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