Results 1 to 1 of 1

Thread: Using model indices in complex model item relationships

  1. #1
    Join Date
    Jan 2010
    Perth, Australia
    Thanked 2 Times in 2 Posts
    Qt products
    Unix/X11 Windows

    Default Re: Using model indices in complex model item relationships

    Given one model index, is it possible to find the model index of another item, if the relationship between the two doesn't match the parent/child structure?

    For example, I have the following:
    Qt Code:
    1. class Person
    2. {
    3. // ...
    5. QString name;
    6. QList<Person*> friends;
    7. }
    9. class PeopleModel : public QAbstractItemModel
    10. {
    11. // ...
    13. QList<Person*> internalData;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Assuming that I have the index to a Person (call him Bob), and all his friends are also in the model's internal data, how do I get the index that corresponds to each of Bob's friends?

    I know how to find the Person* pointer to each friend, but I can't find a way to map this pointer back into the model. The closest thing I could find was QAbstractItemModel::createIndex(), but that function seems to require knowledge of row+column of every Person. Would I need to record the location of each Person in the model?

    Added after 1 35 minutes:

    I think I've got it. Based on the TreeItem in, I can use QList::indexOf() to find the position of the friend-pointer in the model's internal list. If the model was populated systematically, in the same order as the internal list, the pointer's position in the list will correspond to the row number in the model.
    Last edited by hackerNovitiate; 29th June 2011 at 14:30.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Event handlers for QTableView model / selection model.
    By hickscorp in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2011, 17:57
  3. Tree Model Item Highlighter
    By sajis997 in forum Qt Programming
    Replies: 9
    Last Post: 9th January 2011, 17:58
  4. Custom model item deletion (Qt4.4)
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2008, 14:35
  5. Checkable item in tree model
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2007, 11:35

Tags for this Thread


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.