Agreed. As QSqlTableModel implements many things that are ok for me, this looks like an overkill. And since the application uses QSqlRelationalTableModel, I would even need to reimplement this class.
Added after 1 9 minutes:
QSqlQueryModel::setQuery(const QSqlQuery &query) is the function that leads to the lock. In turn, this function calls
void QSqlQueryModelPrivate::prefetch(int limit)
bool QSqlQuery::seek(int index,
bool relative
) bool QSqlCachedResult::fetch(int i)
bool QSqlCachedResult::cacheNext() // called repeatedly until requested record has been fetched
bool QSqliteResult::gotoNext()
bool QSQLiteResultPrivate::fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch)
void fetchMore(const QModelIndex &parent)
void QSqlQueryModelPrivate::prefetch(int limit)
bool QSqlQuery::seek(int index, bool relative)
bool QSqlCachedResult::fetch(int i)
bool QSqlCachedResult::cacheNext() // called repeatedly until requested record has been fetched
bool QSqliteResult::gotoNext()
bool QSQLiteResultPrivate::fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch)
To copy to clipboard, switch view to plain text mode
Only when sqlite3_step(sqlite3_stmt*) returns SQLITE_DONE a call to sqlite3_reset(stmt) is made, i.e., only when there is no more data to fetch. Thus, the lock on the database is only released once all data has been fetched; which is the (undesirable) behaviour I observed.
So, the issue is in the QSQLITE driver. And indeed, after realizing this, I find it described in the Qt docs, see qthelp://org.qt-project.qtsql.520/qtsql/sql-driver.html#general-information-about-qsqlite: "The driver is locked for updates while a select is executed. This may cause problems when using QSqlTableModel because Qt's item views fetch data as needed (with QSqlQuery::fetchMore() in the case of QSqlTableModel)."
Bookmarks