Hello,

I have a SQLite database with a large dataset, around 200000 rows.
These rows are selected from the database with a QSqlQueryModel like this:

Qt Code:
  1. QString q = QString("SELECT name FROM groups");
  2. model->setQuery(q);
To copy to clipboard, switch view to plain text mode 

This only returns the first 255 rows.
Thus I need to use canFetchMore() and fetchMore() in a while loop to get all the rows.

The disadvantage of that is that the userinterface becomes unresponsive for about 5 seconds. I like to solve that in such a way that all the rows are added without the userinterface freezing.



If I don't use the query model and just do a QSqlQuery with the same SELECT command and using while(query.next()), I get all the rows in less than a second. This is my debug output:

Time = QTime("11:35:26")
Number of rows returned: -1
Calculated rows: 192792
Query is active: true
Time = QTime("11:35:26")

This is the code:
Qt Code:
  1. QSqlQuery speedQuery;
  2. qDebug() << "Time = " << QTime::currentTime();
  3. speedQuery.exec("SELECT name FROM groups;");
  4.  
  5. int i = 0;
  6. while(speedQuery.next())
  7. {
  8. ++i;
  9. }
  10.  
  11. qDebug() << "Number of rows returned: " << speedQuery.size();
  12. qDebug() << "Calculated rows: " << i;
  13. qDebug() << "Query is active: " << speedQuery.isActive();
  14. qDebug() << "Time = " << QTime::currentTime();
To copy to clipboard, switch view to plain text mode 

How can I make QSqlQueryModel as fast as QSqlQuery?
I think the speed differences are in inserting rows in the model. Is there a way to speed this up?

If I don't use the canFetchMore() - fetchMore() loop, then there's no speed problem, but the slider on the listview doesn't work correct. If I drag the slider to the bottom, more items are fetched and the more I go down, the slower it becomes.

Can anyone give me any pointers on how to load all the items of the database in the listview without freezing the userinterface for a couple of seconds?