Results 1 to 2 of 2

Thread: Accessing parent IndexModel in a tree Model

  1. #1
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Accessing parent IndexModel in a tree Model

    Hello,

    Is there any difference in accessing indexes' parents through the indexes themselves, with QModelIndex:: parent(), or through the model they belong to, with QAbstractItemModel:: parent(const QModelIndex & index)?
    I have a hard time understanding which of the model or the the indexes should keeep the tree hierachy information?

    Also, since QAbstractItemModel::createIndex(int row, int column, void * ptr = 0) does not offer to specify a parent index when creating a new index, how does an QModelIndex know what parent index it belongs to using QModelIndex:: parent()?

    Thanks,

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Accessing parent IndexModel in a tree Model

    QModelIndex::parent() is implemented using QAbstractItemModel::parent() anyway. QModelIndex keeps a pointer to the model to facilitate this.

    QAbstractItemModel::createIndex() is used internally to a model to generate indexes for items. The pointer or integer data member is often used as a pointer to the internal data structure that represents the item. That model's implementation of parent() can use the internal pointer to find the item representing the index, and from that derive the row QModelIndex of the parent. For a worked example see Ch4 in Advanced Qt Programming by Summerfield.

  3. The following user says thank you to ChrisW67 for this useful post:

    Guett_31 (30th April 2013)

Similar Threads

  1. Accessing complete QWebFrame document tree, not just elements
    By TimShnaider in forum Qt Programming
    Replies: 1
    Last Post: 2nd November 2012, 03:37
  2. Dynamic plugin: accessing parent's methods
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2011, 09:16
  3. QStandardItemModel, parent / child to form tree structure
    By Nightfox in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 17:01
  4. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 06:49
  5. accessing gui elements from parent
    By chemstar in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2006, 11:22

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.