Results 1 to 7 of 7

Thread: QModelIndex value for the root of a tree model

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default QModelIndex value for the root of a tree model

    I’m a bit confused about QModelIndex concerning the fact that you need to have a parent index to get an index value for an item in a tree model. That makes perfect sense if the item your obtaining an index value for, is the child of another item.

    However if you need the index value for a top level item, it is unclear to me about how to obtain or create that value since the parent of top level items is the root and it doesn’t appear that you can obtain a QModelIndex for the root.

    I’ve attempted to make a QModelIndex at row 0 column 0 but since there is no parent for that position, it doesn’t quite work. I have also used a null QModelIndex using QModelIndex() as the parent, and that too, does not appear to work.

    At this point, I’m thinking I may be going about this all wrong, so if anyone can clear up my confusion concerning this issue, I would certainly appreciate it.

    Thanks in advance
    Corny

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QModelIndex value for the root of a tree model

    Have you looked at the Qt Simple Tree Model example? It pretty much explains everything you would need for a non-editable tree model. There is also an example for an editable tree model.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QModelIndex value for the root of a tree model

    Thank you for your prompt reply, and everything you mention is pertinent.

    I may not have sufficiently explained the situation though, so to be a bit more precise, I have re-implemented a QAbstactItemModel and the index and setData functions. When I'm attempting to set the data, I need an index value of that item. To get the index value of a top level item, I need to send the row and column of the item and the index value of the item's parent (which technically is the root) to the index function. That obviously results in an invalid index value being returned.

    Now the problem is sending the index value of the parent of the top level item. If I send QModelIndex() as the parent, the setData() function sees the value as invalid and simply returns a false. I considered just treating any invalid value as representing the root, but thought that might pretty much defeat the purpose.


    Added after 6 minutes:


    d_stranz, thanks for the suggestion. Yes, I have pored over those examples extensively and perhaps due to blindness, didn’t see a solution to my particular situation.
    Last edited by Corny; 19th March 2019 at 21:41.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QModelIndex value for the root of a tree model

    To get the index value of a top level item, I need to send the row and column of the item and the index value of the item's parent (which technically is the root) to the index function.
    Maybe you are confused because you are thinking that a QAbstractItemModel of a tree is like the usual computer science implementation of a tree: that there is an actual node that represents the root of the tree, and that node holds pointers to the top level items in the tree, and so forth.

    In a Qt tree model, there is no root node. It is an imaginary, null QModelIndex, such that the parent of any topmost node in the tree is QModelIndex(). If you call QModelIndex::parent() for any top-level QModelIndex, you get QModelIndex(), for which QModelIndex::isValid() returns false.

    If you ask the model for QAbstractItemModel::rowCount() (with no argument; that is, the default argument QModelIndex()), the correct return value is the number of top-level items. columnCount() for the root should return 0.

    When calling QAbstractItemModel::index() for a top-level item in the tree, you pass a null QModelIndex(). For all other levels of the tree, you pass the result of parent() for that item.

    Remember that QAbstractItemModel really is an abstraction of a tree. There is nothing behind it, and you have to derive from it and wrap the interface around your own data structure. That's one of the reasons why the createIndex() protected method takes a quintptr as its last argument - you can use that to pass a pointer to the item in your internal data structure represented by the index in the Qt tree. You can retrieve that pointer using QModelIndex::internalId() and use it to go back to the appropriate entry in your internal data.

    If I send QModelIndex() as the parent, the setData() function sees the value as invalid and simply returns a false.
    Then you probably haven't implemented setData() correctly (or you are calling it incorrectly). setData() takes a QModelIndex that points to the actual item you want to change, not the parent of that item. If you do pass a null QModelIndex, the setData() should correctly return false because that would mean you are trying to change the imaginary root of the tree. However, if you pass a valid QModelIndex, and that index's parent is null, that's fine - it just means you are trying to change a top-level item.

    Yes, I have pored over those examples extensively and perhaps due to blindness, didn’t see a solution to my particular situation.
    Maybe give a bit more detail about the shape of your tree - what underlying data structure are you trying to represent?
    Last edited by d_stranz; 20th March 2019 at 00:16.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    Corny (20th March 2019)

  6. #5
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QModelIndex value for the root of a tree model

    Thank you!

    That actually clears up some of the confusion I was experiencing. I'm going to attempt a different approach and see what effect that has. I'm fairly confident at this point that I have a better understanding of how this works.

    Again, I want to thank you for clearing up my understanding.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex value for the root of a tree model

    Quote Originally Posted by Corny View Post
    To get the index value of a top level item, I need to send the row and column of the item and the index value of the item's parent (which technically is the root) to the index function. That obviously results in an invalid index value being returned.
    Not obvious at all, this should return a valid index if the model has such a cell:
    Qt Code:
    1. if (model->rowCount(QModelIndex()) > 0 && model->columnCount(QModelIndex()) > 0) {
    2. QModelIndex topLevelRow0Column0 = model->index(0, 0, QModelIndex());
    3. Q_ASSERT(topLevelRow0Column0.isValid());
    4. }
    To copy to clipboard, switch view to plain text mode 
    If the index is not valid despite this cell being part of the model, then there must a problem with the model's index() implementation.

    The index() and the parent() method are the two tricky ones when implementing a tree model, so things can go wrong there.

    See https://wiki.qt.io/Model_Test for a simple standard test harness for models and also check out https://www.kdab.com/development-res...ools/gammaray/ (has a model inspector plugin)

    Cheers,
    _

  8. The following 2 users say thank you to anda_skoa for this useful post:

    Corny (20th March 2019), d_stranz (22nd March 2019)

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex value for the root of a tree model

    Quote Originally Posted by Corny View Post
    I’ve attempted to make a QModelIndex at row 0 column 0 but since there is no parent for that position, it doesn’t quite work. I have also used a null QModelIndex using QModelIndex() as the parent, and that too, does not appear to work.
    The null/empty QModelndex represents the root of a tree model
    Qt Code:
    1. QVariant rootDisplayRoleData = model->data(QModelIndex(), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

    Toplevel nodes therefore have that as their parent
    Qt Code:
    1. QModelIndex topLevelRow0Column0 = model->index(0, 0, QModelIndex());
    2. QVariant displayRoleDataRow0Column0 = model->data(topLevelRow0Column0, Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

    Of course accessing the model's index API directly is rarely necessary, signals carry QModelIndex arguments and delegates also get told which index they are to work on.

    Cheers,
    _

Similar Threads

  1. Replies: 15
    Last Post: 18th February 2016, 10:19
  2. Shows the root of a tree
    By Alundra in forum Qt Programming
    Replies: 12
    Last Post: 4th March 2015, 07:26
  3. Replies: 1
    Last Post: 29th August 2013, 06:41
  4. Not able to remove the root of the tree widget.
    By aurora in forum Qt Programming
    Replies: 1
    Last Post: 30th December 2011, 14:04
  5. QTreeView: include root in tree?
    By meter in forum Newbie
    Replies: 2
    Last Post: 14th June 2010, 13:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.