Results 1 to 5 of 5

Thread: Building a model from an item's children...

  1. #1
    Join Date
    May 2013
    Posts
    34
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Building a model from an item's children...

    Hello,

    I have a QStandardItemModel structure that I've set up to be displayed in a tree view. The first item of each record contains several children, with each child having multiple children of it's own.

    Thus, the tree structure looks something like:

    Top-Level Reccord 1
    | ----> Record-level list 1
    | ----> Record-level list 2
    | ----> List-level item 1
    | ----> List-level item 2
    | ----> Record Level list 3
    Top-Level Record 2
    Top-Level Record 3
    ...

    It's easiest to think of it as each record having several different lists of information, thus the first-level children described the list name / type, and the second level children are the list items.

    What I want to do is create a proxy model that operates *only* on one set of second-level children (a model that manipulates list items directly).

    For example, if I have a record with three lists, and I want to create a model that manages the items in the second list, I should be able to write something like:

    Qt Code:
    1. MyListProxyModel listProxyModel();
    2. listProxyModel.setSourceModel (mainModel);
    3.  
    4. QStandardItem secondListItem = mainModel.item(0, 0)->child(1);
    5. QModelIndex secondListIndex = mainModel.indexFromItem (secondListItem);
    6.  
    7. listProxyModel.setRootIndex (secondListIndex);
    8.  
    9. qDebug() << "The second list has " << listProxyModel.rowCount() << " records...";
    To copy to clipboard, switch view to plain text mode 

    Output: "The second list has 2 records..." (assuming above example structure).

    I can think of a way to implement this in a custom proxy model, but it's not a very appealing approach, and I'd rather not write a custom class to encapsulate this functionality if I don't have to.

    Is there a reasonably clean way to go about accomplishing this?

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

    Default Re: Building a model from an item's children...

    Are you doing this to get a tree subset to display in a tree view? Does QAbstractItemView::setRootIndex() scratch your itch?

  3. #3
    Join Date
    May 2013
    Posts
    34
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Building a model from an item's children...

    Not quite. I'm actually creating a UI framework that's scriptable at runtime. The top-level data describes the layout information loaded from the script, and the child lists are the actual model data that I want to manipulate with the UI widgets. Really, it's two separate models that I'm combining into one - the top-level data declares the UI, the child data is the actual data I want to manipulate.

    Originally, I had these represented as two separate models. I combined them because a tree view provided a nice way to be able to inspect the loaded data all at once for script debugging purposes. Maybe I'm just making this harder than it needs to be...

    So to answer the question, no. I want a QAbstractItemModel way of doing what you can do with QAbstractItemView, I guess...

    My only solution at this point is to create a secondary model which pushes / pulls this list data to / from the primary model. I'd much rather do it with a QSortFilterProxyModel, for example, but unless there's an intrinsic way to filter only the children of a particular index / item, i'm not sure how to get it done without a lot of hassle.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,359
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Building a model from an item's children...

    Another approach would be to have two models working on the same data, just displaying different portions.

    You are currently using QStandardItemModel which is both the model and the data store, but if you have your data in some application data structures it should be possible to use models just as interfaces.

    Cheers,
    _

  5. #5
    Join Date
    May 2013
    Posts
    34
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Building a model from an item's children...

    "You are currently using QStandardItemModel which is both the model and the data store, but if you have your data in some application data structures it should be possible to use models just as interfaces."

    I think that's what I've settled on doing. The child items are built off of a QStringList, so I'll save that list in the parent item as Qt::UserRole data, then retrieve it as needed. Should be fairly simple to get / set the list to / from the item and cause the child items to be rebuilt whenever the parent list is set...

    Thanks for the ideas!

Similar Threads

  1. Replies: 1
    Last Post: 6th January 2014, 10:49
  2. Replies: 9
    Last Post: 14th February 2013, 19:39
  3. Replies: 0
    Last Post: 5th September 2011, 08:08
  4. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 14:30
  5. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 06:49

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.