PDA

View Full Version : Informing a model its data has changed



saknopper
17th January 2007, 17:24
Hi all,

I'm using 2 models (a QSqlRelationalTableModel and a QSqlTableModel) working on the same data. One is used to display only some columns of the table and it allows people to click on the item which causes all the data from the table to be loaded in a different section of the screen using all sorts of widgets (LineEdit, DateEdit etc.).

The problem is that I'd like to inform the model used to display the list of items when the data is changed and saved in the other model. Currently I'm executing a simple select() but this reloads the entire table while only the data in a single row has changed. Is there a way I can optimize this to only make the model reload a single row?

wysota
17th January 2007, 17:26
Use a proxy model instead of the smaller model to select only a subset of the original model data. You can also use the dataChanged() signal from the model to inform the other that some data has changed, but this is tricky as you'll need another layer for translating indexes, so using a proxy model should be much easier.

saknopper
17th January 2007, 19:30
That indeed seems like the way to do it, though I do have a question.

Like I said in my previous post, I have a relation between 2 tables and one model uses it while the other doesn't. That's the reason I created 2 models in the first place. So the QSqlRelationTableModel which is used in the list with only a subset of the columns displayed shows the value of the joined table while the QSqlTableModel uses the id and passes it to a QComboBox.

So how should I get this behaviour with a proxy model? Can I modify the query of the sourcemodel or proxymodel to select both the value from the joined table and the id of the parent table. Is there a best practice to do this?

wysota
17th January 2007, 19:58
You can have data from the other table (referring to the foreign key in the sql model) downloaded into the proxy model and simply "implement" the relation yourself - return data from the other table according to the id taken from the source model.