Results 1 to 5 of 5

Thread: Sum of a column in QSqlTableModel

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sum of a column in QSqlTableModel

    Is it possible to put a query like
    Qt Code:
    1. SELECT SUM(column) FROM mytable
    To copy to clipboard, switch view to plain text mode 

    inside a QSqlTableModel?
    I woud get the sum of table without re-querying the database, because I already have the model...

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

    Default Re: Sum of a column in QSqlTableModel

    Do you want it as a separate row or a separate column or how? I think the best way would be to use a proxy model.

  3. #3
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sum of a column in QSqlTableModel

    This example may help

    Qt Code:
    1. QSqlQuery query;
    2. query.exec("SELECT MAX(id) FROM mytable");
    3. if (query.next()) {
    4. myStringResult = query.value(0).toString();
    5. }
    6. query.finish();
    To copy to clipboard, switch view to plain text mode 

    off course you need to replace the SQL code inside the .exec(_)

  4. #4
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sum of a column in QSqlTableModel

    Ok, but I would use some Qt model.
    If I used a model to connect to the database, I would not to make an sql query.
    There is a way to put compicated query inside the model?

    G

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sum of a column in QSqlTableModel

    You can use QSqlQueryModel.

Similar Threads

  1. QSortFilterProxyModel - lessThan sorts wrong column
    By ghorwin in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 10:06
  2. QTableWidget column width and resizing
    By shooogun in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2008, 23:31
  3. Replies: 0
    Last Post: 10th November 2006, 14:46
  4. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 09:00
  5. How to dispay an icon in the first column of QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2006, 16:51

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.