tags:

views:

396

answers:

2

There is signal in QStandardItemModel which is emitted when data of an item is changed. Usually we connect a handler for this signal and do all the work in the handler routine.

Such handle routine only gets pointer to the item. Using this pointer it is possible to access the data of the item. However, we do not know what exactly has changed... we only have updated value.

If item data has several roles I want to be able to get exactly which role (data) has been changed and what was the previous value.

A: 

This is not possible with the standard signals of Qt. I suggest to add another signal for that.

For my own models, I usually use this approach: I have a root instance which contains pointers to all parts of my data model. Items in my model use this root instance to send signals like

itemChanged(item, attribute, oldValue, newValue)

for simple properties. The same goes for lists and the like; only here, I have several signals depending on the action, for example:

itemAdded(list, item, index)

[EDIT] The QT signal handling is very basic. Usually, it will only tell "something has changed". There is no support for "what exactly has changed?" since you don't need it most of the time. So if you need that information, you must do it yourself. You can't use a role alone, because roles must be backed by something in your item. What you can do is add change information to your items and read that when the role is requested. But this is not something that is supported "out of the box".

Aaron Digulla
Probably I do not understand your approach. This looks too complicated. For me it would be easier to store each OldValue as a separate data with separate role in the item. This way, I only have to compare and update this value for any itemChanged signal and I get all I need. I just thought it should be already there in QT.
AlexKR
+3  A: 

In general, QStandardItemModel is for very simple data modeling. If you want to get into more advanced things like you desribe, you should look into subclassing QAbstractItemModel or one of it's abstract derivatives: Model/View classes

It may seem like a lot of work, but use the examples and refernce guides: Model/View programming, Model subclassing and the rewards will be great.

Adam W
Thank you for the info.However, it was surprise for me that we cannot directly inherit from QStandardItemModel since its members are not virtual. Is it that we always have to implement all stuff from QAbstractItemModel?
AlexKR
Yes, or one of it's abstract derivatives like I said, for example: QAbstractListModel, QAbstractTableModel, QDirModel, QFileSystemModel. Item, list, or table are generally what you use. List and table just make life easier if your data can be represented that way. Somewhere in the Qt documentation there is also a tree model that you can use as reference.
Adam W