Results 1 to 3 of 3

Thread: A Totalling Proxy Model?

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

    Question A Totalling Proxy Model?

    Hi All,

    I'm looking for a nudge in the right direction here.

    I have a base QSqlTableModel containing up to 10000 rows of ~30 columns. 15 of those columns are numeric. The model is generally viewed through a QSortFilterProxyModel with a complex QSortFilterProxyModel::filterAcceptsRow() reimplementation to reduce the data set based on user options. This much works well.

    I'd like to have an elegant way to get a total for each of the numeric columns. I could go directly to the database tables with a query but they do not reflect the current state of data in the model (i.e. add/edit/delete are handled in memory and not immediately followed by submitAll()).

    I've pondered how to get an item model based solution that returns a single row of column totals and can be layered on top of the filter proxy (or base table). It's easy enough to iterate over the model row and columns and build a total when all the rows are there (in setModel()). However, making sure that the totals are updated efficiently is proving difficult. I can catch the
    Qt Code:
    1. void rowsAboutToBeRemoved ( const QModelIndex & parent, int start, int end );
    2. void rowsInserted ( const QModelIndex & parent, int start, int end );
    To copy to clipboard, switch view to plain text mode 
    signals and adjust total accordingly by subtracting or adding column values respectively. Catching:
    Qt Code:
    1. void dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight );
    To copy to clipboard, switch view to plain text mode 
    does not let me see the previous value of the cells affected, so I cannot subtract them from the total before adding the new values. At the moment, any dataChanged() signal needs to completely recalculate the totals in affected columns.

    Can anyone suggest a good/efficient/expedient way to achieve this? Should I just go brute force and recalculate all the totals on any change? Am I missing the blindingly obvious QTotalsProxyModel ?

    Thanks,
    Chris W

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: A Totalling Proxy Model?

    I don't know of a QTotalsProxyModel but have you considered using a custom delegate? If you reimplement QItemDelegate::setModelData(), the new value will be in the editor. You can retrieve the current value from the model with model->data(), calculate the difference, adjust your totals and then call model->setData().

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A Totalling Proxy Model?

    Must admit I hadn't thought of that. I'd have to look at the ramifications of making the delegate dependent on a second model though.
    Last edited by ChrisW67; 7th April 2010 at 04:10.

Similar Threads

  1. How to filter duplicates from a proxy model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 14th October 2016, 11:21
  2. Proxy model, index mapping.
    By wojtekw in forum Qt Programming
    Replies: 14
    Last Post: 7th January 2016, 09:09
  3. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 13:41
  4. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 09:50
  5. Model and Proxy
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2006, 22:05

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.