PDA

View Full Version : QSqlQueryModel data update



psi
21st December 2006, 10:23
This question may have been answered somewhere else already, but I just can't seem to find the answer. I'm using the model/view programming to display a set of data from a sql database. I'm using the QSqlQueryModel to retrieve the data.

The database is empty initially, therefore there is nothing to display in the beginning. As data is coming in, the view doesn't get updated. Btw, the data in the database is inserted by a different thread. I though by setting the model in the view object is enough, but apprently more is needed to be done.

I tried to connect the model's dataChanged() signal to the view's update() or repaint() slot, but no dice. I explictly tested the model's dataChanged signal, and only to find it never sent the signal. I tried to run the another instance of the same program, and the 2nd instance did show the data, but new incoming data still doesn't get displayed.

Isn't the model supposed to be aware of the status of the data it is monitoring? I tried a similar example with QDirModel, the view doesn't get updated when I added or removed files in the folder that the model is monitoring.

Any help is appreciated, thanks.

code:


QSqlQueryModel* mod = new QSqlQueryModel;
mod->setQuery("SELECT id FROM infotable");

QListView* list = new QListView;
list->setModel(mod);
list->show();

wysota
21st December 2006, 13:06
Databases don't notify their clients about a change in their contents, so you have to check it yourself now and then and refresh your model (using select()) when needed. That's how databases work and there is nothing you can do about it.

psi
22nd December 2006, 01:52
I was under the impression that the model classes would monitor the data assigned to them, guess I was wrong. According to what you said, I would just have to use a timer and write something like this?


void timerEvent()(
{
mod->select();
}


Do I need to set the model to refresh the view? Thank you.

ps. this is very strange..... the above code will actually erase the view (ie. the header columns) for some reason.

wysota
22nd December 2006, 09:20
I was under the impression that the model classes would monitor the data assigned to them
In general - yes, but with databases it's simply impossible. The overhead for doing this is quite high.


According to what you said, I would just have to use a timer and write something like this?


void timerEvent()(
{
mod->select();
}

Yes, something like that.


Do I need to set the model to refresh the view?
No, the model will tell the view to refresh itself.


ps. this is very strange..... the above code will actually erase the view (ie. the header columns) for some reason.
Yes. If you use select() you actually clear your model and populate it again, so the header data vanishes as well and you have to set it again. If you wish to avoid that, you have to avoid making a select() and compare and synchronise the model yourself, but it's slightly more work, so it's proabably easier to set the headers again. Note that you're likely to also loose any selection in the view.

jcyangzh
20th July 2012, 03:59
You should use setQuery.