PDA

View Full Version : Sorting columns of the QTableView



Qiieha
29th April 2011, 08:41
Hi all. I need your help please...
I have a QTableView and use the QSqlQueryModel and the QItemSelectionModel to handle the data. Now I want to enable sorting by clicking on the header. I can enable sorting at the TableView, but the sort method desn't work.

Does anybody knows how I can sort my QTableView with less effort. I don't want to use the QTableWidget. It would be great, if that works.

MarekR22
29th April 2011, 09:01
By default model doesn't know how to sort data in effective way, you have to implement it by overriding QAbstractItemModel::sort (http://doc.qt.nokia.com/latest/qabstractitemmodel.html#sort).

Edit: sorry, there is easy solution. Just replace QSqlQueryModel with QSqlTableModel, there is sort method already implemented. You have to just enable sorting in table QTableView::sortingEnabled.

Qiieha
29th April 2011, 09:20
Ok, thank u for your help, but I want to use the setQuery method. And if I use QSqlTableModel this Method isn't available(protected). And the documentation say, that setQuery shouldn't be used.

Do you know a solution?

MarekR22
29th April 2011, 09:36
Which setQuery do you use? Note that void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase()) is still public, only void QSqlQueryModel::setQuery(const QSqlQuery &query) is protected.

Qiieha
29th April 2011, 09:49
Unfortunately QSqlTableModel has only one setQuery() method and it is this one: QSqlQueryModel::setQuery(const QSqlQuery &query).

What can I do? :(

MarekR22
29th April 2011, 10:05
Nope! You are wrong!
QSqlTableModel inharits form QSqlQueryModel. One version of setQuery is repated in QSqlTableModel to make it protected for some reason. Second version is still public and you can and should use it.

Qiieha
29th April 2011, 10:17
I tried to make that and know that there are two versions, but the compiler doesn't offer the possibility to use it :(

Qiieha
9th May 2011, 09:41
Does anybody know why???

spawn9997
9th May 2011, 19:24
This has been asked a lot and it seems there is no easy or direct answer. HERE (http://www.google.com/search?q=sort+QTableview+QSqlQueryModel&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&)

It seems implementing your own model is the best bet. I prefer sorting via proxy (QSortFilterProxyModel) but DB access speed can make large models sort slowly.

spawn9997