How to inform listview that model data has changed?
-
Whats the recommended way to notify the listview that an item in its model has changed?
I would like to not use the layoutChanged() signal, as it seems to be to verbose. I already know which item has changed, and want to signal only this particular item. -
Hi,
You can use "dataChanged":http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#dataChanged with both first and last item pointing to the same index.
Hope it helps
-
Thanks, and how would my model item know about the model its contained in to inform it?
-
I am not sure I am following you correctly:
You do have a custom model right ?
The where does your changes come from ? -
Lets say the changes come from a file system change listener. The display text of the model item should change, when its underlying file changes.
-
If your model is custom, the listener should notify your model that something has changed, then you should update the model content and emit the signal
-
Let me explain using some pseudo code:
@
class ModelItem {
ModelItem(file) {
registerChangeHandler(file, this, &ModelItem::fileChanged)
}void fileChanged(file) {
// How do I update the model, this ModelItem is part of?
}
}class Model : public QAbstractItemModel {
}
@
In fileChanged() how do I update my model, which will then in turn update the UI? -
Are you sure that fileChanged should be in the item and not the model ?
-
Yes, because other items of the Model are not backed by a File, and don't need change listeners. Every item knows better how it should update itself. The Model is just a collection of different items.