PDA

View Full Version : QSqlQueryModel, setQuery and updating attached views



ChrisW67
26th March 2009, 08:01
G'day All,

I currently have a QSqlQueryModel that executes a query to aggregate a number of columns from a single table of potentially a few thousand rows and return a single row of totals. The query is dynamically built to include or exclude rows matching criteria based on a few flags. Every time the flags changes the model builds a query string and uses setQuery() to change its presented data (this process does not lend itself to a parameterised query).

The model drives a simple form through a QDataWidgetMapper. When I create the model, execute setModel() and toFirst() the form is updated with the model's default query values.

When the model flags change the query is changed, no error is raised (the query is good), but the view did not update automatically as I had expected it to. What I've done is to execute reset() in the model after setQuery() and connected the modelReset() signal to the mapper's toFirst() slot.

Is this the 'correct' Qt way to go about it? Are there better signals and slots I've missed?

Alternatively, is there a better way to achieve the goal, perhaps using QSqlTable Model and filtering proxy?

Any and all input gratefully accepted.

wysota
26th March 2009, 08:11
Setting a new query on the model should call reset() by itself so you don't have to do that manually. As QDataWidgetMapper is not a real view, you have to make sure it is told to position itself on a valid index (as you did by calling toFirst()).

I would advise against using a proxy for filtering in your case unless the data in your database is stationary (doesn't change over time). If it is, then using a filter proxy is a better choice than requerying the database. But if the data changes, then you have to refresh the model from the database or else you won't notice changes. Also if you have more than few thousand rows using a filter proxy might become unefficient. SQL DBMS are optimized for filtering data by using indexes, Qt models are not.

ChrisW67
26th March 2009, 08:43
Thanks,

That was how I read the docs too (4.5.0), but if I remove the explicit call to reset() the data mapper does not update (presumably the signal is not sent). I also looked at the requirement that the query be active() and not isForwardOnly(): both OK before and after the setQuery().

It reassuring to know that this newbie is not completely off-beam :)