PDA

View Full Version : Refreshing a QTableView when QAbstractTableModel changes



JPNaude
29th January 2009, 08:15
Hi

I have a few QTableView widgets, all connected to custom QAbstractItemModel based models I created for the different tables. I'm not sure what the best way is to refresh both of them (QTableView and model connected to it) when the data change. At the moment I'm doing the following every time the data changes:



realMatrixModel = new MatrixRealModel(internalRealMatrix);
m_ui->MatrixTable->setModel(realMatrixModel);


I'm not sure if it is optimal, but it sort of works. The problem is that the QTableView does not refresh until I click on it. I've tried repaint(), setFocus() etc. etc. but nothing thus has the same effect as clicking on the QTableView.

How can I do this?

Thanks in advance,
Jaco

faldzip
29th January 2009, 08:33
You should use signal QAbstractItemModel::dataChanged(). Every time data change in your model, it has to emit that signal to notify views and/or proxy models that data has changed. Typicaly it is emitted in QAbstractItemModel::setData() after setting data, as it stands in Qt docs: "The dataChanged() signal should be emitted if the data was successfully set."
All views will refresh changed items.

JPNaude
29th January 2009, 09:13
I did look at dataChanged(). The problem is not when the data change during a setData() call.
The model has an internal pointer that points to a data structure. The application changes this data structure during runtime, thus setData is not called. The problem is that dataChanged() is protected so it does not work unless called inside the model itself. I can probably connect a data structure change signal to a slot in the model that emits the dataChanged() signal, however I thought there should be an easier way.

For example: When I click anywhere on the QTableView after the data structure changed, it updates the QTableView.
Another example: When I add a message box just after the code in my first post the QTableView refreshes.

Does these operations emit the dataChanged() signal? Or is there an easier way?

Thanks
Jaco

wysota
29th January 2009, 09:38
The model has an internal pointer that points to a data structure. The application changes this data structure during runtime, thus setData is not called.
Then make a custom method in your model to change that data so that the application can do it using that method and make that method emit dataChanged().