[Qt-jambi-interest] Displaying the hierarchical structure of an xml file using model/view framework

Gunnar Sletta gunnar at trolltech.com
Tue Mar 11 09:34:48 CET 2008


moebius at altern.org wrote:
> Hello,
> 
> I'm trying to write a small app for displaying the hierarchical structure
> of an xml file using the model/view framework.
> 
> To do this, I have created a XmlTreeModel class inherited from
> QAbstractItemModel to hold the hierarchical model and a QTreeView to
> display it. I have also defined a XmlTreeItem class to handle each single
> xml element.
> Parsing file and building model is achieved in populate(), parseBranch()
> and createItem() functions. For each XmlTreeItem instance, the
> corresponding QModelIndex.internalId is the reference identifier. This
> Long value is stored into the XmlTreeItem instance itself and also into a
> HashMap<Long, XmlTreeItem>. This unique identifier for each XmlTreeItem
> instance is used to retrieve data once the model completely built,
> especially in the QAbstractModelItem data() function.
> 
> Here is the model class code (quite long, sorry) :

Hi,

The QAbstractItemModel is not an ideal class for representing 
hierarchical datastructures, unfortunatly. There was quite some 
discussion about this in the early days of this mailing list and I did 
post an example in this thread:

http://lists.trolltech.com/qt-jambi-interest/2006-12/thread00011-0.html

It uses the same approach as you, with an internalId-index hash and is 
quite slow because of it ;-/

I didn't look thouroughly at your code, but I suggest that you look at 
your parent() function. That is usually where I usually have problem in 
the QAbstractItemModel.

Because of the lack of usability and some performance problems with 
QAbstractItemModel in Java when used as a QTreeModel we implemented 
com.trolltech.qt.gui.QTreeModel. Did you look at it? With it you can map 
your XMLNodes directly rather than using the QModelIndex indirection.

Hope this helps,

Gunnar


More information about the Qt-jambi-interest mailing list