View Full Version : QAbstractItemModel container members?

9th December 2009, 17:02

I've just started using QAbstractItemModel and QModelIndex with the intent of storing data that I will subsquently display in a QTableWidget subclass and also manipulate in other ways etc. I sort of expected that QAbstractItemModel would contain its own (private) container members so that I could just enter in and retrieve cell data with accessor members like setData(...)/data(). (Like wxGrid and wxGridTableBase.) However, given how data(), rowCount(), and columnCount() are pure virtual, and SetData(...) "... must be reimplemented for editable models." (documentation), and finally that QModelIndex::data() just refers back to its QAbstractItemModel, it looks like this is not true. I.e., I have to create my own container member, such as a vector, to store data values, and implement the above-named functions to access/manipulate *that*.

Is this right? If so then I kind of wonder what QAbstractItemModel is for--i.e., why not just create my own little TableDataStorage class with a vector, setData()/data() functions, and use that? Or to put it another way--do you guys in general find QAbstractItemModel to be useful? Thanks--


13th December 2009, 12:33
the QAbstract*Model classes do not contain any internal data store. They are meant to be subclassed to offer a model abstraction over an existing data store and that's what make them so useful : you can use model view without altering or converting your existing data structures
these classes cannot be used in conjunction with Q(Table|List|Tree)Widget but only with Q(Table|List|Tree)View because the former use an internal subclass of QAbstract*Model classes with their own data store.
it would be just fine to create a model class using vectors as data storage. Just make sure you also offer an alternate way of accessing that data (i.e not through model/view API) for use in other parts of your program.

hope this helps

13th December 2009, 17:42
The QAbstract*Model classes are just defining consistence interfaces for those View classes to use.
Think of that. How can we write a general view class withou those abstract models? What model type should we put there for the setModel funtion?

13th December 2009, 19:47
Thanks guys--since my last post, after a tour through the Qt source, I have managed to figure out how these work, and I'm starting to appreciate their usefulness. Probably more so as I start to get into big datasets that I need to do math on.