Results 1 to 7 of 7

Thread: QModelIndex value for the root of a tree model

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 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; 19th March 2019 at 23: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.

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

    Corny (19th March 2019)

Similar Threads

  1. Replies: 15
    Last Post: 18th February 2016, 09:19
  2. Shows the root of a tree
    By Alundra in forum Qt Programming
    Replies: 12
    Last Post: 4th March 2015, 06:26
  3. Replies: 1
    Last Post: 29th August 2013, 05: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, 13:04
  5. QTreeView: include root in tree?
    By meter in forum Newbie
    Replies: 2
    Last Post: 14th June 2010, 12: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
  •  
Qt is a trademark of The Qt Company.