PDA

View Full Version : QTableView update



inktomi
15th July 2009, 17:22
I subclassed QAbstractTableModel and implemented rowCount(), columnCount() and data(). The actual data is stored in a QList outside the model and the model has a pointer to the QList. Data manipulation of the QList occurs outside the model (I'm not using setData).

In order to update the view, I implemented a method in the model that emits the dataChanged signal. The problem is that the view is not being updated.


void ClipListTableModel::updateViews(QModelIndex start, QModelIndex end)
{
emit dataChanged(start, end);
}

The start and end values are retrieved as follows. The table has 5 rows, hence the 4 in the argument.


QModelIndex in1 = clipListModel->index(row, 0, QModelIndex());
QModelIndex in2 = clipListModel->index(row, 4, QModelIndex());
clipListModel->updateViews( in1, in2 );

To make sure that the model is working correctly, I used setModel() each time I wanted an update. Everything worked fine this way.

jpn
15th July 2009, 20:15
Notice that whenever you add/remove rows/columns to/from the model you need to call beginInsertRows() and endInsertRows() (or the corresponding ones depending on the action).

inktomi
16th July 2009, 09:16
Notice that whenever you add/remove rows/columns to/from the model you need to call beginInsertRows() and endInsertRows() (or the corresponding ones depending on the action).

Does this mean that modification to the data has to be done through the model? If some data is tied to more than one model, then it doesn't make sense to modify the data through all the models.

Please correct me if I'm wrong, and thank you for your reply.

jpn
16th July 2009, 12:33
Does this mean that modification to the data has to be done through the model?
It doesn't HAVE to be. You can do similar workarounds as you did for emitting dataChanged(). In a way or other, beginInsertRows() needs to be called before you start inserting the rows, and endInsertRows() needs to be called when you're done inserting the rows. BUT these all are protected methods of the model and it sounds like thing are getting out of your hands if you intend to control all this from outside the model.


If some data is tied to more than one model, then it doesn't make sense to modify the data through all the models.
To me it sounds like a good moment to take the design back on the drawing board... :) At least to me it would sound like a better idea to hide the data list inside a model. For controlling the data, you could have as convenient API as you want, you wouldn't have to use QAbstractItemModel's insertRows() or other methods from outside the model. Your data model class would just implement QAbstractItemModel interface as a bonus, and it could be attached to a number of views thanks to that.

inktomi
16th July 2009, 15:18
I made the neccesary changes so that the data is modified through the model.

Thank you.