Welcome!

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

Reply
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?

Developer
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:

https://developer.blackberry.com/cascades/documentation/ui/lists/groupdatamodel.html

On the bottom of this page there's an example on how to use it:

https://developer.blackberry.com/cascades/documentation/device_platform/data_access/using_data_sourc... 

 

 

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:

http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-a-Custom-DataModel-to-Fi...

 


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.