Results 1 to 5 of 5

Thread: Aggregating/Summarizing a Model's Data

  1. #1
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Aggregating/Summarizing a Model's Data

    Possible duplicate: http://www.qtcentre.org/threads/1493...a-model-s-data

    I have a QStandardItemModel of items where each row corresponds to a single object and each column corresponds to a property of that object. Here's an example using a student ID roster:

    Qt Code:
    1. ID Gender Grade GPA
    2. 5 Male 12 3.8
    3. 4 Female 11 3.9
    4. 8 Female 12 3.5
    5. 19 Male 9 2.1
    To copy to clipboard, switch view to plain text mode 
    My end goal is to let the user select one of the column names from a drop-down and have a second table display an aggregate of the values. For example, if the user selected Grade from the list, the second table would display the average GPA for each grade as follows:

    Qt Code:
    1. ID Gender Grade GPA
    2. 12 3.65
    3. 11 3.9
    4. 9 2.1
    To copy to clipboard, switch view to plain text mode 
    Or similarly by Gender:

    Qt Code:
    1. ID Gender Grade GPA
    2. Male 2.95
    3. Female 3.70
    To copy to clipboard, switch view to plain text mode 

    Is there anything in Qt that performs that functionality? QSortFilterProxyModel seems to come close in terms of functionality, but as far as I can tell it only culls and sorts items, not summarizes them.

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

    Default Re: Aggregating/Summarizing a Model's Data

    You have to provide your own QAbstractProxyModel implementation that performs such calculations. For performance reasons you probably want to calculate aggregates on request and cache them in the proxy model instead of recalculating them each time someone asks for data.
    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. The following user says thank you to wysota for this useful post:

    Phlucious (14th February 2013)

  4. #3
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Aggregating/Summarizing a Model's Data

    Thanks for the reply! I like the idea of a proxy model, but I don't understand the implementation. Wouldn't the proxy model have to create its own items to display the aggregates that are related to the original model items? My current understanding of proxy models is that they only filter/sort/re-format existing items.

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

    Default Re: Aggregating/Summarizing a Model's Data

    Quote Originally Posted by Phlucious View Post
    Wouldn't the proxy model have to create its own items to display the aggregates that are related to the original model items?
    Yes but that doesn't change anything. QSortFilterProxyModel also creates some internal data for the proxied model.

    My current understanding of proxy models is that they only filter/sort/re-format existing items.
    They can manipulate the model in any way.

    If you don't feel comfortable with calling it a "proxy", you can implement it as a custom model completely independent of the model it observes. Just connect to appropriate signals of the original model and you're good to go.
    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.


  6. The following user says thank you to wysota for this useful post:

    Phlucious (14th February 2013)

  7. #5
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Aggregating/Summarizing a Model's Data

    Another thread helped me to understand that I've been approaching the model-view concept from an item-based approach and that had I never actually made the plunge to thinking about it in terms of models, so what you say now makes a lot more sense. Thanks for your help!

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Replies: 1
    Last Post: 24th February 2011, 05:54
  3. Aggregating a model's data
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2008, 14:59
  4. Model - data
    By gyre in forum Newbie
    Replies: 1
    Last Post: 9th December 2007, 19:49
  5. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14

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.