1 Database + 2 Models + 2 Views
How do I get two QSqlTableModels, both using the same database, to update based on the other? I may be doing this wrong, but I am using a QDataWidgetMapper to drive a set of widgets on one dialog. So that means I need a model that is always looking at the same table in the database.
I have another view that I want to be able to change tables, so that requires a different model, as far as I know. I am using the same database connection for both models...perhaps this is wrong.
I presume that I can't use the same model to display a different table in two different views.
When the mapped ui items update their model, I would like the values to be updated in the second model (if it has the same table selected, obviously). And vice-versa. However this doesn't happen.
How do I accomplish this?
I have tried reselecting both the models but this doesn't seem to help.
Many thanks for your help!
Both the models are set to use QSqlTableModel::OnFieldChange as their editStrategy
The mapper is set to DataWidgetMapper::AutoSubmit
Interestingly when the non-mapped tableview is edited, the results are seen in the mapped widgets, but only if I use the previous and next buttons I've set up to call toNext and toPrevious. I.e if I'm at the first record, I hit next, but it takes me to the new first record rather than the second.
I've created two slots for the dataChanged signal of each model, and neither gets hit..?
When the mapped ui items update their model, I would like the values to be updated in the second model (if it has the same table selected, obviously). And vice-versa. However this doesn’t happen.
How do I accomplish this?
Unfortunately, there's no standard way a SQL model can know when the underlying table was modified (by another model, or in general by some external modification). You have to write a workaround.
- If your DB supports notifications, you can use QSqlDriver::subscribeToNotification and call "NOTIFY" in a trigger, which will cause the QSqlDriver::notification signal emission; in a slot, you can simply ask the model to select() again.
- If your DB doesn't, you can for instance make some trigger to add a row in a table, and from your Qt code you poll that table.
OK. Thanks for the those. Notify sounds like a way forwards.
I've been trying a few other options, following is the simplest:
If I am using the mapper I now set the other view to use the same model. So now there's one model, two views. Simples, right?
One view is a tableview, the other a set of widgets using a mapper.
Still, things are not as I expect. I followed the books example, and connected the tableview rowchanged signal to the mappers setcurrentmodelindex. This changes the selection in the mapper if I select a row in the tableview.
If I alter anything in the tableview, it is reflected in the mapper widgets, when I click in a different cell (not when I hit enter)
But if I change anything in the mapper widgets, only the first change is reflected in the tableview. Ideas?
Edit: working now. I changed to QSqlRelationalTableModel from QSqlTableModel and all seems fine.