How do I scroll to the last line of the SQLITE model in the QTableView?

I have a Sqlite model with a few thousand rows connected to a QTableView. I can view the rows in the QTableView, but when I scroll to the bottom of the QTableView, I can only reach the first few hundred The end of the row. If I continue to pull the scroll bar button, the new row is attached to the view, but I cannot easily scroll to the end. After attaching all the rows to the view, I can easily scroll from top to bottom without problems .

This is the important part of the code (standard only):

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 miss something very simple. Any suggestions?

Lawrence.

QSqlTableModel lazily loads data, that is, it will load data when requested Load the project. If the project needs to be displayed, QTableView will ask for the project. The delay you observe is the part where QSqlTableModel gets new data from the database.

There is also an issue, if the SQL driver does not report Query size, 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 doesn’t actually know how many items there will be until all items are loaded.

To eliminate the delay, you should load all data in advance (as shown in fetchMore):

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)

I have a Sqlite model with a few thousand rows connected to a QTableView. I can view the rows in the QTableView, but when I scroll to the bottom of the QTableView, I can only reach the first few hundred The end of the row. If I continue to pull the scroll bar button, the new row will be appended to the view , But I cannot easily scroll to the end. After attaching all the rows to the view, I can easily scroll from top to bottom without problems.

This is the important part of the code (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 miss something very simple. Any suggestions?

Lawrence.

QSqlTableModel loads data lazily, that is, it loads the item when requested. If the item needs to be displayed, QTableView will ask for the item The delay you observe is part of QSqlTableModel getting new data from the database.

There is also an issue, if the SQL driver does not report the query size, 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 .

Therefore, the model does not actually know how many items there will be before loading all the items.

In order to eliminate the delay, you should load all the data beforehand (e.g. < code>fetchMore):

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)

Leave a Comment

Your email address will not be published.