Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to last row of a Sqlite model in a QTableView?

I have a Sqlite model of several 1000 rows attached to a QTableView. I can view the rows in the QTableView but when I scroll to the bottom of the QTableView, I can only get to the end of the first few 100 rows. If I keep pulling on the scrollbar button, new rows get appended to the view but I cannot easily scroll to the end. Once all rows are appended to the view, I can then easily scroll from top to bottom without problem.

Here's the important part of the code (which is just standard):

self.db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(dbFileName)

self.model = QtSql.QSqlTableModel(self.db)
self.model.setTable("results")
self.model.select()
self.tableViewResults.setModel(self.model)

I must be missing something very simple. Any suggestions?

Laurence.

like image 652
Lozzer Avatar asked Feb 15 '13 04:02

Lozzer


1 Answers

QSqlTableModel loads data lazily, i.e. it will load the items when it's asked for. And QTableView will ask for items if it needs to display them. That delay you observe is the part that QSqlTableModel fetches new data from the database.

There is also an issue, if SQL drivers doesn't report Query size, and SQLite is one of them:

If the database doesn't return the number of selected rows in a query, the model will fetch rows incrementally. See fetchMore() for more information.

So the model actually doesn't know how many items there will be until it loads all the items.

In order to eliminate the delay you should load all data beforehand (as fetchMore suggests):

self.db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(dbFileName)

self.model = QtSql.QSqlTableModel(self.db)
self.model.setTable("results")
self.model.select()
while self.model.canFetchMore():
    self.model.fetchMore()
self.tableViewResults.setModel(self.model)
like image 166
Avaris Avatar answered Sep 25 '22 09:09

Avaris