I have a QTableView with a QSortFilterProxyModel as its model which has a QSqlRelationalTableModel as its source model.
When I sort or filter the viewed data through the QSortFilterProxyModel, only the fetched data is affected.
I managed to get it work by fetching all data from the table by using
if model.canfetchMore():
model.fetchMore()
The problem is the table has a large number of records (200,000 records).
Is there any other way to sort and filter the proxymodel without fetching all the data from the database?
You probably work with Sqlite as you have to use
fetchMore()
.When you work with large number of records, it's slow to load 200.000 records or so from database at once, and it's not
fetchMore()
that's problematic, but just loading that quantity of data from database is slow. It is slow even when you work with PostgreSQL where you don't have to usefetchMore()
. So I use a trick to have all records, but don't have to wait too long. I split (the same) table into two views, I call it parent and child:It is possible only if you have an attribute (or a prefix of an attribute, or more than one attributes) that you can use as a "category".
This program loads slowly, but only database input. Once database is full, the program works fast. This is just an example, in real app you have your database full.
With this trick, you can handle 1.000.000 records, just use 1.000 x 1.000 (or 200 x 5.000, or some other combination). The problem is that search and filter are harder to implement this way. I showed one way how to implement search.
Regarding "Why don't you implement filtering and sorting using SQL" comment, that's because sql can only return data in one order and one (fixed) filter, and with
QSortFilterProxyModel
you can sort and filter data additionally any way you like and way faster than using sql once you have your data in models.