Results 1 to 3 of 3

Thread: Sum of rows in QSqlTableModel

  1. #1
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Sum of rows in QSqlTableModel

    Dear All,

    I have a table like this,

    id(int) value(double) documentid(string)
    -----------------------------------------------
    sum(value)

    What I would like to is, after I set filter to my model, I would like to insert a row with the sum(value) in the table. Is there any way that I could do this?

  2. #2
    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: Sum of rows in QSqlTableModel

    I think you might need to derive a custom QSqlTableModel and reimplement the data() method and rowCount() methods().

    Your rowCount() method should return:

    Qt Code:
    1. QSqlTableModel::rowCount() + 1 // (for the sum row)
    To copy to clipboard, switch view to plain text mode 

    For all row and column values of the QModelIndex passed to the data() method except the ones corresponding to the summation row, simply return the value from QSqlTableModel::data(). Then in the last row, the column containing the "Total" string should return the corresponding QString in the variant, and the actual total column returns the sum as a long or floating point QVariant.

    Something like this (not tested...):

    Qt Code:
    1. QVariant MySqlTableModel::data( const QModelIndex & index, int role )
    2. {
    3. if ( !index.isValid() )
    4. return v;
    5.  
    6. long nDataRows = QSqlTableModel::rowCount();
    7. if ( index.row() < nDataRows )
    8. v = QSqlTableModel::data( index, role );
    9.  
    10. if ( role == Qt::DisplayRole )
    11. {
    12. if ( index.row() == nDataRows ) // OK, because we add an extra row for the sum
    13. {
    14. if ( index.column() == 0 )
    15. v = QString( tr("Total") );
    16. else if ( index.column() == 1 )
    17. v = long( sumOfValues() );
    18. }
    19. }
    20. return v;
    21. }
    To copy to clipboard, switch view to plain text mode 

    You'll need to implement the "sumOfValues()" or issue another database query to return the sum.

  3. #3
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sum of rows in QSqlTableModel

    or you cold do something like this (I'm assuming you filtered by documentid):

    Qt Code:
    1. QSqlQuery q("SELECT SUM(value) FROM yourtable WHERE documentid='" + "whatever" + "'");
    2. if(q.next())
    3. {
    4. ui->le_mysum->setText("%1").arg(q.value(0).toDouble());
    5. }
    To copy to clipboard, switch view to plain text mode 

    I didn't check the above code for syntax, but I hope you get idea.

    I would add the sum in a line edit (or label) just below the table like this:
    showtotal.jpg

Similar Threads

  1. Replies: 2
    Last Post: 25th January 2010, 20:56
  2. QSqlTableModel
    By codeman in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 11:04
  3. How to map the rows to sourceModel rows.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2009, 08:40
  4. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47
  5. QSqlTableModel and LIKE
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 08:45

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.