Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I do not understand when to use QAbstractItemModel::createIndex()
Forum Updated to NodeBB v4.3 + New Features

I do not understand when to use QAbstractItemModel::createIndex()

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 563 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    This question inspired by my answer at https://forum.qt.io/post/817523. Although I believe my answer to the OP there is correct, I (apparently like him too) do not understand when one needs to use createIndex(), as opposed to just index() itself.

    In have written proxy models where I override map{From,To}Source(), and my code works, but it doesn't mean I understand!

    In, say, https://codebrowser.dev/qt6/qtbase/src/corelib/itemmodels/qidentityproxymodel.cpp.html#_ZNK19QIdentityProxyModel11mapToSourceERK11QModelIndex we see map{From,To}Source() using createIndex/createSourceIndex() respectively.

    But in, say, https://stackoverflow.com/a/18130955/489865 we see those methods using index/sourceModel()->index() instead.

    Could someone explain what calling createIndex() versus index() is all about? Preferably in terms I can understand :)

    1 Reply Last reply
    0
    • JonBJ JonB referenced this topic on
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      In short - index() is a public function which can and should be called from outside to get a proper index for the given row, column and parent.
      createIndex() is the internal function which actually creates a new QModelIndex() with the given row, column and some internal data to e.g. build up the parent-child tree structure for a QTreeView. Some models also store a pointer (or similar) to the actual data structure behind this model. E.g. the id to the database row so when calling data() one could do a lookup in a QMap by this id.
      so index() normally calls createIndex() after some sanity checks (see e.g. QStandardItemModel

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        In short - index() is a public function which can and should be called from outside to get a proper index for the given row, column and parent.
        createIndex() is the internal function which actually creates a new QModelIndex() with the given row, column and some internal data to e.g. build up the parent-child tree structure for a QTreeView. Some models also store a pointer (or similar) to the actual data structure behind this model. E.g. the id to the database row so when calling data() one could do a lookup in a QMap by this id.
        so index() normally calls createIndex() after some sanity checks (see e.g. QStandardItemModel

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Christian-Ehrlicher
        Fair enough. It's just that in those two links, both doing a proxy model derived from a base, one calls index() in map{From,To}Source() while the other calls createIndex(). Not clear which then is the best or right one.

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          The mapToSource in the StackOverflow answer is incorrect. Not because it calls index but because it returns an index from the proxy model and not from the source model.
          It should be sourceModel->index(...) instead of index(...).

          createIndex is potentially cheaper to call, but for that you'd have to now what should the internal pointer be, so it is not always possible to do so. index would set the internal pointer correctly.

          Call createIndex when you can (or need), call index instead.
          In a tree model the internal pointer is needed to retrieve where you are in the tree hierarchy from an index, in a flat table or list model it is not needed but might be used for optimization.

          1 Reply Last reply
          4

          • Login

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