01-08-2013 10:18 AM
QListDataModel doesn't expose any iterators like QList does e.g. begin() and end() which are needed for either std::sort or qsort. I looked into trying to use the QList Iterator but data model doesn't expose it's data object in a way that I can see that would allow it to work with a standard iterator.
Does anyone have any suggestions as to how I can sort a QListDataModel?
01-08-2013 10:48 AM - edited 01-08-2013 10:50 AM
GroupDataModel has built-in sorting functionality:
On the bottom of this page there's an example on how to use it:
For QListDataModel you can try to implement your own iterators mapping to dataModel's insert/move/removeAt, but I think this isn't the best approach because the dataModel emits signals every time an item is moved or updated so ListView can refresh itself.
I think it's better to make a copy of DataModel's internal data, sort it as QList then clear() the dataModel and append() the data back. You might have to iterate the dataModel appending entries to QList using size() & value() methods.
Or wrap the dataModel into another one for sorting so the data is not copied. This is more complex to implement:
01-08-2013 02:36 PM
I ended up just creating my own model from the base DataModel and using a QList internally. It provides all the methods I need.
I was hoping not to have to implement the boiler plate code for the DataModel methods with QListDataModel, but it ends up being a tiny bit of extra code overall.
I did look at implementing my own iterator but the QListDataModel is not really setup for accessing it's internal data in an iterator friendly way. Plus coding an iterator would have taken much longer than converting the model as I did.