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. TreeModel: hasIndex() vs. isValid()
Forum Updated to NodeBB v4.3 + New Features

TreeModel: hasIndex() vs. isValid()

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.3k 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.
  • B Offline
    B Offline
    Binary91 0
    wrote on last edited by
    #1

    Hi,

    I'm creating my first TreeModel as it is explained in THIS example.

    In the QModelIndex TreeModel::index(int iRow, int iColumn, const QModelIndex &parent)function, in a first step there is a check whether the given index does exist inside the TreeModel:

        if(!this->hasIndex(iRow, iColumn, parent))
            return QModelIndex();
    

    If this check returns true, then I think parent must be a valid index, otherwise the model would not return true when asking for the index, right?
    If so, then I'm asking myself why in the example there is a second check for validity of parent:

        if(!parent.isValid())
            parentItem = rootItem;
        else
            parentItem = static_cast<myQTreeModelItem*>(parent.internalPointer());
    

    Is this second check really needed?

    Thank you in anticipation.

    Greetings, Binary

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Top level items have no parent, so if an index is valid and its parent is not then this means the item is top level (root child in case of a tree structure). In other words in this example root is represented as an invalid index.

      B 1 Reply Last reply
      3
      • Chris KawaC Chris Kawa

        Top level items have no parent, so if an index is valid and its parent is not then this means the item is top level (root child in case of a tree structure). In other words in this example root is represented as an invalid index.

        B Offline
        B Offline
        Binary91 0
        wrote on last edited by
        #3

        @Chris-Kawa Ah, I understand. Root index is invalid, I forgot that.

        Thank you!

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Binary91 0
          wrote on last edited by Binary91 0
          #4

          Hi again,

          this topic was already marked as solved, but I need to know something else about QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const:
          This function should check whether the given index exists in the model, right?
          When I'm checking the source code of this function in qabstractitemmodel.cpp, it only checks if the given row and column do exist inside the given model index:

              return row < rowCount(parent) && column < columnCount(parent);
          

          , but it does not check whether the model index itsself refers to the model, right?
          Shouldn't be there a check like:

              return row < rowCount(parent) && column < columnCount(parent) && parent.model == this;
          

          ? Otherwise I would still not know if the requested index was really part of my model or just part of the given index that may refer to another model...

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            No since column/rowCount(parent) is using the correct model (the one from parent). Also QModelIndex() can be invalid.

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

            B 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              No since column/rowCount(parent) is using the correct model (the one from parent). Also QModelIndex() can be invalid.

              B Offline
              B Offline
              Binary91 0
              wrote on last edited by Binary91 0
              #6

              @Christian-Ehrlicher This is the rowCount() implementation in the Tree Model example:

              int TreeModel::rowCount(const QModelIndex &parent) const
              {
                  TreeItem *parentItem;
                  if (parent.column() > 0)
                      return 0;
              
                  if (!parent.isValid())
                      parentItem = rootItem;
                  else
                      parentItem = static_cast<TreeItem*>(parent.internalPointer());
              
                  return parentItem->childCount();
              }
              

              There is also no check whether the given model index refers to the "this" model or not. Right? So, if the given model index refers to an external model item that accidentally fits the given parameters (rows, columns), then the hasIndex function should return true, right? It does not check if the model is the current model or another one? Am I wrong?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Binary91-0 said in TreeModel: hasIndex() vs. isValid():

                Am I wrong?

                The function will return a maybe wrong value when you pass a wrong parameter, yes - you can add an assert in your implementations if your model is used from outside or if you don't trust yourself.

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

                B 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Binary91-0 said in TreeModel: hasIndex() vs. isValid():

                  Am I wrong?

                  The function will return a maybe wrong value when you pass a wrong parameter, yes - you can add an assert in your implementations if your model is used from outside or if you don't trust yourself.

                  B Offline
                  B Offline
                  Binary91 0
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Alright, thank you, I just wanted to know if it was possible.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    It is possible and I think it's not checked since the programmer has to take care to not pass a wrong QModelIndex to the functions. The model is rarely checked, mostly only in SQFPM code due to this assumption.

                    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
                    0
                    • B Offline
                      B Offline
                      Binary91 0
                      wrote on last edited by
                      #10

                      Ok, I see. I think I need to get more further used to the whole topic to understand how the model works and what duties are on the programmer side and what should be handled by the model itsself.

                      Thank you for the support.

                      Greetings

                      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