[Qt-interest] simple tree model example

Stephen Kelly steveire at gmail.com
Fri May 21 19:05:35 CEST 2010


Mandeep Sandhu wrote:

> Anyone? An inputs...at least on the Troll Q? :P
> 
> Thanks,
> -mandeep
> 
> 
> On Thu, May 20, 2010 at 4:32 PM, Mandeep Sandhu
> <mandeepsandhu.chd at gmail.com> wrote:
>> Hi All,
>>
>> While reading up the model-view programming docs, I came across the
>> simple tree model example which illustrates how to use the abstract
>> model for implementing a hierarchical tree like data (since there's no
>> existing class (like QListview/QTableView) which can handle such
>> category of data).
>>
>> http://doc.qt.nokia.com/4.6/itemviews-simpletreemodel.html
>>
>> While going through the other available model classes, I came across
>> QStandardItemModel/QStandardItem which seems to be capable of holding
>> tree like data structures.
>>
>> Is QStandardItemModel, the right model for holding tree-like data? If
>> so, why wasn't this model used in the simple tree model?

Two reasons to implement QAbstractItemModel are:

1) You don't need to keep everything in memory. Using the QStandardItem API 
you need to keep everything in memory, but using QAbstractItemModel you can 
implement lazy loading, and probably something more efficient that 
QStandardItemModel for your specific use case.
2) You use a QAIM to implement an interface to another data structure (which 
is often a good idea) and that data structure can update itself (for example 
if it is a local cache of networked data) it makes more sense from a design 
point of view to update the model from the 'inside'. Using 
QStandardItemModel you would have to update it from the outside by listening 
to the signals possibly in a subclass of QStandardItemModel and inserting 
and updating items through the QStandardItemModel API. It is probably also 
faster to update the model from the inside if you know certain things about 
the data structure you are modelling.

Using QAIM also gives you scope to do some other tricks, but if neither of 
those things above are a concern to you, QStandardItemModel is probably the 
right choice. You still have the advantages of being able to represent 
custom data objects (QStandardItem::data is virtual) and being able to use 
proxy models.

This is also discussed in an article I have drafted and hope to finish soon. 
Taster: 'Compromise' :)

>>
>> I have used QStandardItemModel populated with my tree data and applied
>> a QTreeView to it and everything has worked fine till now.
>>
>> Just wondering why, in the simple tree model example, they used the
>> QAbstractItermModel as base and not QStandardItemModel. The model-view
>> programming guide states:
>>
>> "QStandardItemModel manages more complex tree structures of items,
>> each of which can contain arbitrary data."
>>
>> Hence my confusion.
>>
>> Thanks,
>> -mandeep
>>
>> PS: BTW one OT question - who are "Trolls"? ppl who work on QT itself?
>> or anybody who uses QT, like app devs? Just curious. :)

Trolltech used to be the company that produces Qt. Nokia bought Trolltech 2 
(?) years ago. Before that people who worked there were called 'The Trolls'. 
People who were around before that still call former Trolltech employees by 
that label sometimes.


All the best,

Steve.

>>





More information about the Qt-interest-old mailing list