Arno Duvenhage
Arno Duvenhage

Reputation: 1960

What is the most appropriate Qt container for QAbstractListModel and QListView

I created a QListView model by inheriting from QAbstractListModel and using QVector as the internal container. My model only allows text to be appended to the back of the list and also only allows items to be erased from the front. The erase operations are batched (i.e. I erase about 10% of items from the front every time the list gets too big).

What is the best container to use?

Currently I'm using QVector, as I expect the performance (contiguous memory layout) to be better in this case compared to something like QList. I am however unsure how Qt accesses the container internally and whether the container being contiguous makes any difference.

Upvotes: 0

Views: 352

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25155

In most QAbstractItemModel subclasses, the most important method to optimize is usually data() (and index(), but that's rarely a problem): It will be called a gazillion times, especially when the views on top have sorting enabled. So copying big chunks of data out of a container or doing non-constant time lookups e.g. in a map is a bad idea. QVector/std::vector is a good standard choice.

So getting a feel which of item model methods are called how often is a good exercise, and can add some counters and see.

Apart from the fast lookups, I think the choice depends mainly on how your code modifies the container, which is independent from it being used inside QAbstractItemModel. Qt doesn't "access the container internally", all access are implemented by you, as you implement data(), rowCount() etc. Also, at some point optimizing the container more will also become pointless, because the view, not the model, will become the bottleneck.

Upvotes: 2

Related Questions