ListView won't get updated when I change data in my custom model
-
I've decided to go down the road of creating a custom model for a part of my application, so that I can easily reuse it with components like ListView. I can't get it to work. The ListView won't ever get updated - it will show my model's initial data forever.
My model inherits from QAbstractListModel. Here is a list of the methods that I've reimplemented, following the documentation's suggestions:
- roleNames
- rowCount
- data
- flags
- insertRows
- removeRows
Here is a list of the signals that I emit in the appropriate places, following the documentation's suggestions:
- dataChanged
- beginInsertRows
- endInsertRows
I'm afraid that this is not enough to implement a simple custom model. What am I missing here? What is needed for a ListView to notice that my data has changed?
-
Are you sure you're calling dataChanged() with the correct values?
-
@Christian-Ehrlicher I pass two QModelIndex objects, one initiated to the top left row and column of my change and one initiated to its bottom right. Via debugger, I can see that the values make sense. For example:
My model has 4 columns (fixed, doesn't change). If I append a new row and my model previously had 2 rows, What I see is:
topLeft -> 2, 0 bottomRight -> 2, 3
-
And is the parent correct?
-
@Christian-Ehrlicher my model is created once upon startup in main.cpp. I create it with my QGuiApplication as the parent. My ListView is also a child of this QGuiApplication. Is this the wrong way to do it?
-
The QModelndex you're passing to dataChanged() need a proper parent index: http://doc.qt.io/qt-5/model-view-programming.html#parents-of-items
-
@VRonin Maybe I've expressed myself badly, since I'm still a newbie in Qt. The ListView is created via QML. It's as basic as:
ListView { id: list model: myModel }
Maybe, when done like that, the parent of the ListView is not the application itself, which must be why you were surprised.
-
Can we finally see where and how you emit dataChanged()? And with which indexes?