Results 1 to 4 of 4

Thread: treeview with multiple models

  1. #1
    Join Date
    Jun 2012
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default treeview with multiple models

    I would like to concatenate multiple models into one tree view. When an index is selected in this treeview, I would like to be able to know what 'sub'model was selected and what index of that 'sub'model was selected.

    Qt Code:
    1. # example data
    2. Model1
    3. data1
    4. child 1
    5. child 2
    6.  
    7. Model2:
    8. data2
    9. child 1
    10. child 2
    11.  
    12. Treeview:
    13. data 1
    14. child 1
    15. child 2
    16. data 2
    17. child 1
    18. child 2
    To copy to clipboard, switch view to plain text mode 

    Purpose: If I clicked on child 2 of data 2, I would like to be able to call setModel(Model2) and setRootIndex(???) on a QTableView which I have set up as an editor.


    If I implement a proxy view, what modifications do I need to make? See below:


    Qt Code:
    1. QSortFilterProxy or QAbstractProxyModel?
    2.  
    3. data(): how do I know which model was clicked?
    4. index(): how do I know which model to use?
    5. parent(): ?
    6. mapToSource(): ?
    7. mapFromSource(): ?
    8. rowCount(): sum of rowCount from all models
    9. columnCount(): 1
    To copy to clipboard, switch view to plain text mode 

    I'm thinking the key is in mapToSource/mapFromSource and having to count what row your on. Eg: If Model 1 has 10 rows and you are on row 12, and Model 2 as 10 rows, then you are requesting row 2 from model 2. Is this how it's done or is there a more elegant approach?

    TYVM,

    BB
    Last edited by BreakBad; 24th July 2012 at 22:31.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: treeview with multiple models

    an QAbstractProxyModel can still only have one model!

    Probably your best bet is to make another model that is a composition of your individual models.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: treeview with multiple models

    If you cannot change the mode 1 and model 2, then your approach for proxy model sounds ok. The better solution would be to have all the indexes (all 20 rows) in a single model.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Jun 2012
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: treeview with multiple models

    Thanks for the replies!

    The only reason I cannot have the data in a single model is because those models are being used elsewhere and I require the data in all my views to be sync'd.

    I did finally find an example in the following thread which I will be exploring: http://www.qtcentre.org/threads/4534...ght=multimodel

Similar Threads

  1. QTreeView and multiple models
    By Daher in forum Qt Programming
    Replies: 3
    Last Post: 3rd December 2019, 20:57
  2. QTreeView with multiple QDir models
    By papillon in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2011, 23:23
  3. QTreeView with multiple models
    By Daher in forum Qt Programming
    Replies: 5
    Last Post: 18th October 2011, 10:53
  4. View using multiple models
    By johnmauer in forum Qt Programming
    Replies: 13
    Last Post: 5th October 2010, 18:28
  5. QSortFilterProxyModel with multiple source models
    By hoshy in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2009, 20:59

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.