[Solved] The "best" way to programmatically refresh a QSqlQueryModel when the content of the query changes
-
wrote on 18 Oct 2010, 16:40 last edited by
I would also guess that the performance impact very much depends on how the Qt database driver is implemented and the internals of the database itself.
I also agree that setQuery typically should not have a huge performance impact but why should this not be encapsulated in the Qt model itself. In my impression a simple public refresh method just seems to be missing.
To give a an example I use the situation I came across when issuing this message.
I use a rather complex SQL query to an Oracle Database to retrieve data that will be used in a graph.
The graph will be updated every second using a timer with the latest data and update the graph.
The columns and the query are constant and I would only like to "refresh" the data will the latest values. -
wrote on 18 Oct 2010, 18:30 last edited by
Another way is to prepare a database view of your complex query and then use a QSqlTableModel on the view. This class does expose an exec() method to re-run the query.
-
wrote on 18 Oct 2010, 18:33 last edited by
Oops sorry, the method is actually select() not exec(). /me should check the docs.
-
wrote on 6 Nov 2010, 08:04 last edited by
QSqlTableModel::select() use setQuery( QString, QSqlDatabase ) method refresh contents. I guess you could do it the same way.
-
wrote on 19 Apr 2013, 12:14 last edited by
Hi all
I have similar question I am use QTableView + QSqlQuery when I set query at first time I allow fetch only first 10 rows from query, after that according user decides I can fetch next few rows, but my QTableView is not refreshed after fetch. I can't use setQuery because I do not what run sql yet one time to get result that I already have.
I try to use something like this
@emit dataChanged(createIndex(OldRowCnt, 1), createIndex(RowCount, HeaderCount));@
but this has no resultthank you for future help )
-
wrote on 22 Apr 2013, 10:39 last edited by
beginInsertRows solve my problem, I am call it after fetch
@bool cOciQModel::fetch(int Cnt, bool SilentMode) {
if (RowCount && !query->seek(RowCount - 1)) {
LastError = QString("Can't goto [%1] position").arg(QString::number(RowCount - 1));
return false;
}
int OldRowCnt = RowCount;
int Count = Cnt + 1;
while (--Count && !AllDataFetched) {
AllDataFetched = !query->next();
if (!AllDataFetched) {
++RowCount;
}
}if (!SilentMode) {
beginInsertRows(QModelIndex(), OldRowCnt, RowCount);
endInsertRows();
}return true;
}@
-
wrote on 24 Dec 2015, 13:11 last edited by
How I update QSqlQueryModel:
QString queryStr = model->query().executedQuery(); model->clear(); model->query().clear(); model->setQuery(queryStr);
-
How I update QSqlQueryModel:
QString queryStr = model->query().executedQuery(); model->clear(); model->query().clear(); model->setQuery(queryStr);
wrote on 20 Jan 2017, 11:36 last edited by@iamantony how to do it with Python?
-
@iamantony how to do it with Python?
@Gelo Replace -> with .
-
wrote on 20 Jan 2017, 14:51 last edited by
QString queryStr = model->query().executedQuery();
model->clear();
model->query().clear();
model->setQuery(queryStr);is that c++? for what is QString?
-
QString queryStr = model->query().executedQuery();
model->clear();
model->query().clear();
model->setQuery(queryStr);is that c++? for what is QString?
@Gelo Yes, it is C++.
QString is a Qt data type for strings - no need to write it in Python.