PDA

View Full Version : The "best" way to refresh a QSqlQueryModel when the content of the query changes



doberkofler
6th April 2010, 15:17
I was wondering what the "most" correct way to refresh a QTableView using a QSqlQueryModel would be when the result of the query would change?

I'm aware that:
1) QSqlQueryModel ::setQuery can be called but why should I parse a new query just to refresh it?
2) QTableView::setModel can be used but why should I set a new model only to refresh it?
3) QSqlQueryModel ::reset can be used but this is a protected method and would completely reset the model including information about the header line etc.

The model/view architecture of Qt seems to have a very complex and well documented architecture but I just cannot seem to find the "correct" way to do this.

Thank you,
D

borisbn
6th April 2010, 15:55
QAbstractItemModel::layoutChanged signal (http://qt.nokia.com/doc/4.6/qabstractitemmodel.html#layoutChanged)

doberkofler
6th April 2010, 18:37
QAbstractItemModel::layoutChanged signal (http://qt.nokia.com/doc/4.6/qabstractitemmodel.html#layoutChanged)

When looking at the documentation of this signal (This signal is emitted whenever the layout of items exposed by the model has changed) it rather looks as if this signal would be emitted when the layout of the model is changed by the model to its views.
It does not seem as if this would be what I'm looking for.

waynew
6th April 2010, 23:19
Try QSqlTableModel instead, you can still use setQuery(), but you can also use model->select() to re-run it.

doberkofler
7th April 2010, 06:19
QSqlTable::select() would be exactly what I need but QSqlTable has it'slimitations.
setQuery in QSqlTableModel is protected and according to the documentation setQuery should not be used.
"This function simply calls QSqlQueryModel::setQuery(query). You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query"

waynew
8th April 2010, 02:19
Very good. Thanks for that information. Checking my project code, I see that I am only using setQuery() where I am using QSqlQueryModel, never with QSqlTableModel.

doberkofler
8th April 2010, 07:43
I'm still wondering what the "best" way for the refresh itself would be or is setQuery simply the only one?

quarkthesecond
3rd April 2013, 18:53
A little bit late, but in case someone comes across this thread in search for a solution (just like me a few minutes ago):

model->query().exec()
This appears to be working well, I've even got two proxy models in between.