Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

New Developer
Posts: 18
Registered: ‎01-22-2010
My Device: BB 9000

Sorting QListDataModel

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?

Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Sorting QListDataModel

[ Edited ]

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:



Andrey Fidrya, @zmeyc on twitter
New Developer
Posts: 18
Registered: ‎01-22-2010
My Device: BB 9000

Re: Sorting QListDataModel

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.