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. How does createIndex know about parent
QtWS25 Last Chance

How does createIndex know about parent

Scheduled Pinned Locked Moved Solved General and Desktop
qmodelidexcreateindexparent indexinternal pointe
7 Posts 3 Posters 2.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.
  • T Offline
    T Offline
    tonnerkiller
    wrote on 5 Apr 2020, 05:14 last edited by tonnerkiller 4 May 2020, 05:14
    #1

    I'm trying to understant QAbstractItemModel::createIndex(). I looked into the Simple Tree Model example and if I understand correctly I feed it with the row, the column and a pointer to my internal data structure for the item in question.

    What I cannot wrap my head around is the following: As a QModelIndex consists of row, column and parent Index, how does the newly created Index know about the parent index when I don't pass one as parameter?

    1 Reply Last reply
    2
    • T tonnerkiller
      5 Apr 2020, 20:37

      Well, yes, I, that is the model, does know about its internals. But still I do not understand (please bear with me).

      Let me rephrase and be a bit more verbose:
      The docs say:

      Each index is located in a given row and column, and may have a parent index

      So I expect the QModelIndex to know about these things: row, column, parent index.

      Now I tried to understand more by looking at the Simple Tree Model Exampel. There I find the following Code for the index method:

      QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
      {
          if (!hasIndex(row, column, parent))
              return QModelIndex();
      
          TreeItem *parentItem;
      
          if (!parent.isValid())
              parentItem = rootItem;
          else
              parentItem = static_cast<TreeItem*>(parent.internalPointer());
      
          TreeItem *childItem = parentItem->child(row);
          if (childItem)
              return createIndex(row, column, childItem);
          return QModelIndex();
      }
      

      I pass into it a column, a row and the pointer to the parent index to have it create me a QModelIndex. Now these parameters are used in the method to determine the demanded internal data. I understand that.
      Where I have problems following is when the actual QModelIndex is created and returned by the use of createIndex(). It is passed the row, the column and the pointer to the internal data, but it is not passed any info on the parent of the item, only row and column.
      Now I imagine calling index() from outside the model and get a QModelIndex object. I imagine using that ModelIndexand call its parent() method. How would the index object know about it? It has never been given this info,right?
      I also found no method in the docs that would allow me to set the parent afterwards, so I guess it must be set at creation, but still it is not one of the parameters passed to createIndex().
      Or do I miss something completely (I guess it is so because I have not seen this question asked before so I'm maybe the only one with this problem comprehending).
      So please if you have any clue what my misunderstanding is and can explain me how things really work, help me.

      Thanks a lot.

      T Offline
      T Offline
      tonnerkiller
      wrote on 6 Apr 2020, 03:03 last edited by
      #6

      @tonnerkiller OK, some more reading in the docs lead me to the solution of my problem:
      QModelIndex does not itself know about the parent index of an item, but of course it knows about the model. So obviously QModelIndex, when queried about the parent index of its item will just call the model's parent() method.

      Of course the model's parent() method cannot rely on the passed QModelIndex's parent() method, but needs to go over the pointer to the internal item, so the internal item should be organised in a way that its parent is always clear to the model.

      Sorry for bothering, I just should have read more at the right place.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 5 Apr 2020, 05:59 last edited by
        #2

        @tonnerkiller said in How does createIndex know about parent:

        I don't pass one as parameter?

        Then you're using it wrong.

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

        T 1 Reply Last reply 5 Apr 2020, 15:16
        1
        • C Christian Ehrlicher
          5 Apr 2020, 05:59

          @tonnerkiller said in How does createIndex know about parent:

          I don't pass one as parameter?

          Then you're using it wrong.

          T Offline
          T Offline
          tonnerkiller
          wrote on 5 Apr 2020, 15:16 last edited by
          #3

          @Christian-Ehrlicher Where would I pass it? As I see it I pass row, column and a pointer to the item. Nothing to the parent. At least this is what I see in simle tree example and what I read in the docs. There is no pointer to the parent index, only to the internal item.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 5 Apr 2020, 15:44 last edited by
            #4

            You (= the model) must know the stuff so the internal pointer points to your tree data structure in the common case. Therefore you know the parent.

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

            1 Reply Last reply
            1
            • T Offline
              T Offline
              tonnerkiller
              wrote on 5 Apr 2020, 20:37 last edited by tonnerkiller 4 May 2020, 20:39
              #5

              Well, yes, I, that is the model, does know about its internals. But still I do not understand (please bear with me).

              Let me rephrase and be a bit more verbose:
              The docs say:

              Each index is located in a given row and column, and may have a parent index

              So I expect the QModelIndex to know about these things: row, column, parent index.

              Now I tried to understand more by looking at the Simple Tree Model Exampel. There I find the following Code for the index method:

              QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
              {
                  if (!hasIndex(row, column, parent))
                      return QModelIndex();
              
                  TreeItem *parentItem;
              
                  if (!parent.isValid())
                      parentItem = rootItem;
                  else
                      parentItem = static_cast<TreeItem*>(parent.internalPointer());
              
                  TreeItem *childItem = parentItem->child(row);
                  if (childItem)
                      return createIndex(row, column, childItem);
                  return QModelIndex();
              }
              

              I pass into it a column, a row and the pointer to the parent index to have it create me a QModelIndex. Now these parameters are used in the method to determine the demanded internal data. I understand that.
              Where I have problems following is when the actual QModelIndex is created and returned by the use of createIndex(). It is passed the row, the column and the pointer to the internal data, but it is not passed any info on the parent of the item, only row and column.
              Now I imagine calling index() from outside the model and get a QModelIndex object. I imagine using that ModelIndexand call its parent() method. How would the index object know about it? It has never been given this info,right?
              I also found no method in the docs that would allow me to set the parent afterwards, so I guess it must be set at creation, but still it is not one of the parameters passed to createIndex().
              Or do I miss something completely (I guess it is so because I have not seen this question asked before so I'm maybe the only one with this problem comprehending).
              So please if you have any clue what my misunderstanding is and can explain me how things really work, help me.

              Thanks a lot.

              T 1 Reply Last reply 6 Apr 2020, 03:03
              1
              • T tonnerkiller
                5 Apr 2020, 20:37

                Well, yes, I, that is the model, does know about its internals. But still I do not understand (please bear with me).

                Let me rephrase and be a bit more verbose:
                The docs say:

                Each index is located in a given row and column, and may have a parent index

                So I expect the QModelIndex to know about these things: row, column, parent index.

                Now I tried to understand more by looking at the Simple Tree Model Exampel. There I find the following Code for the index method:

                QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
                {
                    if (!hasIndex(row, column, parent))
                        return QModelIndex();
                
                    TreeItem *parentItem;
                
                    if (!parent.isValid())
                        parentItem = rootItem;
                    else
                        parentItem = static_cast<TreeItem*>(parent.internalPointer());
                
                    TreeItem *childItem = parentItem->child(row);
                    if (childItem)
                        return createIndex(row, column, childItem);
                    return QModelIndex();
                }
                

                I pass into it a column, a row and the pointer to the parent index to have it create me a QModelIndex. Now these parameters are used in the method to determine the demanded internal data. I understand that.
                Where I have problems following is when the actual QModelIndex is created and returned by the use of createIndex(). It is passed the row, the column and the pointer to the internal data, but it is not passed any info on the parent of the item, only row and column.
                Now I imagine calling index() from outside the model and get a QModelIndex object. I imagine using that ModelIndexand call its parent() method. How would the index object know about it? It has never been given this info,right?
                I also found no method in the docs that would allow me to set the parent afterwards, so I guess it must be set at creation, but still it is not one of the parameters passed to createIndex().
                Or do I miss something completely (I guess it is so because I have not seen this question asked before so I'm maybe the only one with this problem comprehending).
                So please if you have any clue what my misunderstanding is and can explain me how things really work, help me.

                Thanks a lot.

                T Offline
                T Offline
                tonnerkiller
                wrote on 6 Apr 2020, 03:03 last edited by
                #6

                @tonnerkiller OK, some more reading in the docs lead me to the solution of my problem:
                QModelIndex does not itself know about the parent index of an item, but of course it knows about the model. So obviously QModelIndex, when queried about the parent index of its item will just call the model's parent() method.

                Of course the model's parent() method cannot rely on the passed QModelIndex's parent() method, but needs to go over the pointer to the internal item, so the internal item should be organised in a way that its parent is always clear to the model.

                Sorry for bothering, I just should have read more at the right place.

                1 Reply Last reply
                1
                • H Offline
                  H Offline
                  HForest
                  wrote on 1 Aug 2022, 23:38 last edited by
                  #7

                  For the record, this was a perfectly good question. The QAbstractItemModel::createIndex() docs do not state that it embeds a model pointer into the index it creates, nor do the QModelIndex docs mention the private constructor that makes it possible.

                  This makes QModelIndex::parent() look suspicious if you happen to be troubleshooting a problem. If you want to be sure your model is implemented correctly, you have to put aside the docs and dig up the inline function definitions in qabstractitemmodel.h.

                  1 Reply Last reply
                  1

                  • Login

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