Results 1 to 10 of 10

Thread: ProxyModel or 'own' Data Model?

  1. #1
    Join Date
    Nov 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default ProxyModel or 'own' Data Model?

    Hi!

    I was already searching the net a lot but couldn't find an answer to my actual problem. So I'll try here...

    I have a simple data-structure, a two-dimensional List (here very reduced to keep it simple): A QList<event>, every event contains a timestamp and a QList<items>, every item just text + value

    Now in the window a have 2 views, a QTreeView and a QTableView

    I've set up a data model for the TreeView and the TreeView displays the data as a tree of depth 2 (works nice):
    Qt Code:
    1. -timestamp, ...
    2. item1 3
    3. item2 1
    4. item5 8
    5.  
    6. -timestamp, ...
    7. item1 1
    8. item3 1
    9. item5 5
    To copy to clipboard, switch view to plain text mode 

    Now the TableView shall display summary calculations of the same data in the form
    Qt Code:
    1. ITEM SUM
    2. ------------------
    3. item1 4
    4. item2 1
    5. item3 1
    6. item5 13
    To copy to clipboard, switch view to plain text mode 

    Now I wonder which approach would be the "right" one? Use a ProxyModel to "map" the TreeModel to the TableModel and do the summary calculations in the ProxyModel?
    Or give the TableView a TableModel which "uses" the TreeModel's data?

    Thanks for any answers!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: ProxyModel or 'own' Data Model?

    Now I wonder which approach would be the "right" one? Use a ProxyModel to "map" the TreeModel to the TableModel and do the summary calculations in the ProxyModel?
    Or give the TableView a TableModel which "uses" the TreeModel's data?
    In my view a TableModel that uses the TreeModels data IS a proxy model, just I guess you mean doing it manually (populating your TableModel manually).
    I would go with QSortFilterProxyModel - it will allow you to concentrate on the important stuff - the end result in your table view.
    You can filter all the multiple occurrences, and while doing it counting them as well.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ProxyModel or 'own' Data Model?

    I would go for two models working on the same data, no need for either to know the other.

    They are both interpreting the data in a different way, not just structuring it differently.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ProxyModel or 'own' Data Model?

    I vote with anda_skoa. I have written code that "flattens" a multi-level tree model into a table, and it is very hard to get it right, especially if you want to handle selections from either the table or the tree in a reciprocal manner (e.g. selecting in the tree selects the corresponding rows in the table and vice versa). If the underlying data is simple, it is better to create two models to work off it.

    There is a kptflatproxymodel in KDE that does map a tree into a table, so if you really want to go that way you could probably adapt it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: ProxyModel or 'own' Data Model?

    especially if you want to handle selections from either the table or the tree in a reciprocal manner (e.g. selecting in the tree selects the corresponding rows in the table and vice versa).
    This argument is not clear to me, if anything its an argument to use a proxy.
    The reason being that if you have two separate models, you have to take care of selection on both of them - and since a proxy uses the same underlying items from the source model, selecting in the proxy selects in the original as well.
    Other draw backs for two models is harder to test, harder to maintain, and adds another bug source (the mapping code).
    But at the end there is no "right answer" - it all depends on your needs and situation.
    Probably in this case, what every seems easier is the better choice if its not a production or large project.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ProxyModel or 'own' Data Model?

    This argument is not clear to me, if anything its an argument to use a proxy.
    Which is in fact what I did, an adaptation of the KDE flat proxy. It's been a while, but my recollection is that the KDE proxy did not have support for mutual selection, so getting all that to work properly was painful. If I was to do it again, I would probably not use a proxy and implement some sort of external mapping.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ProxyModel or 'own' Data Model?

    Quote Originally Posted by high_flyer View Post
    and since a proxy uses the same underlying items from the source model, selecting in the proxy selects in the original as well.
    Each view would work with a different model (one with the proxy, one directly with the source model), their QItemSelectionModel instances would work with different indexes, no?

    Cheers,
    _

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ProxyModel or 'own' Data Model?

    Each view would work with a different model (one with the proxy, one directly with the source model), their QItemSelectionModel instances would work with different indexes, no?
    Exactly, and this is one of the difficulties in synchronization - you need to map one index back up to the original tree model and then back down to the corresponding index in the table model (if selecting from the tree) or from the table model up to the tree model (if selecting from the table).

    My actual use case is worse than that - not only do I have a tree -> table flattening, I also allow sorting and rearrangement of table view columns as well as additional proxies and views that I use to plot pairs of columns from the table in graphs. So selecting from the tree means mapping to the sorted table proxy to the flat table proxy and then to each of the proxies that filters the flat table for the graphs. Selecting a point from a graph involves reversing that whole process to highlight the corresponding original item in the tree.

    Nearly two years later, I'm still trying to track down bugs.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: ProxyModel or 'own' Data Model?

    Indeed you are right guys.
    Found this interesting thread about it, might be helpful for the OP:
    https://forum.qt.io/topic/6139/best-...ltiple-views/5
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. The following user says thank you to high_flyer for this useful post:

    d_stranz (20th February 2017)

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ProxyModel or 'own' Data Model?

    Thanks for the really useful link.
    Last edited by d_stranz; 20th February 2017 at 18:45.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 4
    Last Post: 21st October 2013, 22:24
  2. Replies: 9
    Last Post: 14th February 2013, 20:39
  3. Replies: 0
    Last Post: 2nd November 2011, 12:13
  4. Replies: 1
    Last Post: 24th February 2011, 06:54
  5. Sharing Selections between Model and ProxyModel
    By mentat in forum Qt Programming
    Replies: 14
    Last Post: 27th January 2010, 18:31

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.