PDA

View Full Version : Trouble with sql views and sorting



Tiansen
1st February 2014, 21:43
Inside QTableView I want to show data from Sql view that fetches data from multiple tables.
I tried two approaches, but both have their own problems:

1. First approach: I used QSqlQueryModel in combination with QTableView. Data are shown OK, but sorting is not implemented in QSqlQueryModel, so that sorting inside QTableView does not work even if it is enabled with sortingEnabled()

2. Second approach: I tried to use QSqlTableModel in combination with QTableView. I think that sorting should work now, but there is no data shown. It is really strange, because data are shown if I fetch it from SQL table, but if I use SQL view, then data are not shown. I thought that for Qt it does not matter if it fetches data from table or from view.

Any idea how to get data from view AND have sorting enabled?

viulskiez
2nd February 2014, 21:16
Hi Tiansen, I think you need a proxy model. The easiest solution might be using QSortFilterProxyModel and keep your QSqlQueryModel instance as the source model.

Tiansen
14th February 2014, 14:34
Very good, thank you for that tip.