Results 1 to 12 of 12

Thread: Group / Aggregate QAbstractItemModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    139
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 59 Times in 52 Posts

    Default Re: Group / Aggregate QAbstractItemModel

    Here is another simpler, although far less efficient way of doing it:
    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlDatabase>
    3. #include <QDebug>
    4. #include <QSqlTableModel>
    5. #include <QAbstractItemModel>
    6. #include <QStringList>
    7. #include <QSqlQuery>
    8. #include <QSqlQueryModel>
    9. #include <QStandardItemModel>
    10. #include <QTreeView>
    11. #include <QHeaderView>
    12.  
    13.  
    14. /* Given a data model, a column to group by, a column to aggregate and an
    15.   aggregate function, this function will return a model with the given
    16.   transformation. Available aggregate function include SUM, AVG, COUNT, MAX
    17.   and MIN.
    18.  */
    19. unsigned int groupByColumn,
    20. unsigned int aggregateColumn,
    21. const QString & aggregateFunction = "SUM")
    22. {
    23. /* Open an in-memory database. */
    24. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    25. db.setDatabaseName(":memory:");
    26. bool ok = db.open();
    27.  
    28. /* Create a table with the correct number of columns. */
    29. QStringList columns;
    30. for (int i = 0; i < m.columnCount(); i++)
    31. columns.push_back(QString("F%1").arg(i));
    32.  
    33. QString sql = QString("CREATE TABLE T1(%1)").arg(columns.join(","));
    34. QSqlQuery query(db);
    35. ok = query.exec(sql);
    36.  
    37. /* Set up a table model of our newly created table. */
    38. QSqlTableModel tbl(0, db);
    39. tbl.setTable("T1");
    40. tbl.setEditStrategy(QSqlTableModel::OnManualSubmit);
    41. tbl.insertRows(0, m.rowCount());
    42.  
    43. /* Now copy accross our model to the newly created table. */
    44. for (int i = 0; i < m.rowCount(); i++)
    45. for (int j = 0; j < m.columnCount(); j++)
    46. tbl.setData(tbl.index(i, j), m.data(m.index(i, j)));
    47.  
    48. /* Submit changes to database. */
    49. ok = tbl.submitAll();
    50.  
    51. /* Finally, query our database for the grouped by table. */
    52. QString sql2 = QString("SELECT %3(F%2), * FROM T1 GROUP BY F%1").
    53. arg(groupByColumn).
    54. arg(aggregateColumn).
    55. arg(aggregateFunction);
    56.  
    57. out->setQuery(sql2, db);
    58. return out;
    59. }
    60.  
    61.  
    62.  
    63.  
    64. int main(int argc, char * argv[])
    65. {
    66. QApplication a(argc, argv);
    67.  
    68.  
    69. QStandardItemModel model(8, 3);
    70. for (int row = 0; row < 4; ++row)
    71. {
    72. QStandardItem *item = new QStandardItem(QString("test"));
    73. model.setItem(row, 0, item);
    74.  
    75. item = new QStandardItem(QString("%1").arg(1));
    76. model.setItem(row, 1, item);
    77.  
    78. item = new QStandardItem(QString("row %0").arg(row));
    79. model.setItem(row, 2, item);
    80. }
    81.  
    82. for (int row = 4; row < 8; ++row)
    83. {
    84. QStandardItem *item = new QStandardItem(QString("test2"));
    85. model.setItem(row, 0, item);
    86.  
    87. item = new QStandardItem(QString("%1").arg(5));
    88. model.setItem(row, 1, item);
    89.  
    90. item = new QStandardItem(QString("row %0").arg(row));
    91. model.setItem(row, 2, item);
    92. }
    93.  
    94. QAbstractItemModel * tbl = doGroupby(model, 0, 1, "SUM");
    95. tbl->setHeaderData(0, Qt::Horizontal, "Total");
    96. tv.setModel(tbl);
    97. tv.header()->moveSection(0, 3);
    98. tv.show();
    99.  
    100. return a.exec();
    101. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by numbat; 10th January 2010 at 18:51.

  2. #2
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Group / Aggregate QAbstractItemModel

    Hi numbat
    It just compiled the zip and it works like a charm! Last time I added the two lines inside the class scope so that's why I could''t get it to work before. I must say I'm impressed with your post - so little code and yet so powerfull. This is exactly what I wanted to do. Thanks!

  3. #3
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Group / Aggregate QAbstractItemModel

    Hi numbat
    It just compiled the zip and it works like a charm! Last time I added the two lines inside the class scope so that's why I could''t get it to work before. I must say I'm impressed with your post - so little code and yet so powerfull. This is exactly what I wanted to do. Thanks!

Similar Threads

  1. How to make QAbstractItemModel 's data checkable
    By nifei in forum Qt Programming
    Replies: 12
    Last Post: 1st April 2013, 19:52
  2. Replies: 2
    Last Post: 22nd September 2008, 09:22

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
  •  
Qt is a trademark of The Qt Company.