QML TableView with custom QAbstractListModel issue
-
Hi,
I have a C++ model extending QAbstractListModel and I have expose this model to QML TableView. The model has got three rows but in the TableView it is displayed as one row. I noticed this as all the three rows were displayed in white color(background) and then the empty rows were displayed with alternating colours. I started debugging this QML by adding a custom row delegate and printed out styleData.row to see if the row index is changing and to my suspicion it is always zero. So I realised that may be something is wrong with my C++ model, replaced my custom model with a dummy list model in QML and it seems to display rows in alternate colours and styleData.row gets incremented in custom row delegate for each row. Anyone has had the same issue or suggest a possible solution/workaround?
Cheers
-
Hi and welcome,
It would help a lot if you could post some of your code, like your implementation of the QAbstractListModel. I usually run into these kinds of problems, and it is often just a silly mistake. Another pair of eyes usually does the trick in locating the bug.
-
I figured out what the problem was - My ListModel did not directly derive from QAbstractListModel. It derived from a base class that handled display roles for common attributes of a base model. In my TableView I was not using the base roles as they were not required but for some reason TableView expects the number of roles in the list model to match the columns. As a workaround I cleared the roles in my derived class constructor and removed call to BaseListModelClass::getRoleData() from derived class getRoleData and this fixed the problem. Hope this helps for other users.