Results 1 to 5 of 5

Thread: QModelIndex::parent question

  1. #1
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QModelIndex::parent question

    It is my first attempt to get a tree model going and I am stuck - don't quite understand how to implement method:

    QModelIndex QAbstractItemModel::parent ( const QModelIndex& index ) const

    My model displays QList<Apartments> as root items, where each apartment has one or more occupants. When I construct an index, reimplementing method

    QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const

    I construct the index with createIndex(row, column, Pointer), where Pointer is a pointer to either Apartment or Occupant. I know what to use, Apartment or Occupant, depending on parent.isValid() call.

    My question is: when I have to construct parent for a given index in the QAbstractItemModel::parent - how do I know if I am getting a root item (Apartment) to which I have to return QModelIndex() or a child Item (Occupant) to which I have to construct a parent?

    Any help is greatly appreciated

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QModelIndex::parent question

    Derive both Apartment and Occupant from the common base class and either use dynamic_cast or implement virtual methods isOccupant() and isApartment() in the base class and reimplement them in the subclasses properly so that you can determine the type of the object by calling one of these.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QModelIndex::parent question

    I was afraid that would be the answer, hoping that I missed something in the documentation.

    So, one has to rely on RTTI to construct a model for a tree view.. Sad.

    I have another, kind of a follow-up question.

    I am under the assumption (did not actually test it myself) that all view models are "virtual", in a sense that the Qt framework will not ask model for a complete range of QModelIndexes.

    Suppose I have 10^6 Occupants in the city and my QTreeView has only two visible Apartments with 5 occupants total. Will my model be a subject to 10^6 or more calls to ::index and :: parent methods? If yes - what is the Qt-way to implement virtual models?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QModelIndex::parent question

    Quote Originally Posted by QPlace View Post
    So, one has to rely on RTTI to construct a model for a tree view.. Sad.
    Not really. But with your current design you have to either use RTTI or virtual methods.

    I am under the assumption (did not actually test it myself) that all view models are "virtual", in a sense that the Qt framework will not ask model for a complete range of QModelIndexes.

    Suppose I have 10^6 Occupants in the city and my QTreeView has only two visible Apartments with 5 occupants total. Will my model be a subject to 10^6 or more calls to ::index and :: parent methods? If yes - what is the Qt-way to implement virtual models?
    The view will only ask for the indexes it currently needs. Just remember it might need them all if you, for example, force the width of a column to be resized to the size of its contents.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QModelIndex::parent question

    Thank you for the help and clarifications.

Similar Threads

  1. Useless but curious compiler warning question
    By Raccoon29 in forum General Programming
    Replies: 4
    Last Post: 30th July 2008, 20:46
  2. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  3. Replies: 1
    Last Post: 15th March 2007, 20:45
  4. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34

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.