QSqlTableModel & QtableView , how to sort by column?
-
Im trying to find what is the easy way to preform sorting on single table im Qurying from SQLite (im using all Qt build class's )
i have QSqlTableModel delegated (as singletone) class that im setting into QTableview
like this:
@ui->PlayList_tableView->resizeColumnsToContents();
ui->PlayList_tableView->setSortingEnabled(true);
ui->PlayList_tableView->setAlternatingRowColors(true);
ui->PlayList_tableView->setModel(PlayListSqlModel::instance()); // this is where i set the model @
and acurding to this "link ":http://www.qtforum.org/article/26898/how-to-sort-data-by-column-in-a-table-view-model-set-for-table-view-is-qsqlquerymodel-if-click-to-headerview.html
the sorting suppose to happen automatically , i do see the little sorting triangles in the headers but there is no sorting .
what im doing wrong here ? -
try using QSortFilterProxyModel
an example:
@ QSortFilterProxyModel *sort_filter = new QSortFilterProxyModel(this);
sort_filter->setSourceModel(model);
sort_filter->sort (0);
this->ui->data_table->setModel (sort_filter);
@ -
Had the same problem and this worked! Thank you KillGabio.