tags:

views:

509

answers:

2

I'm a (Py)Qt newbie, porting C# GUI code to Qt for a couple of days now. One question that I keep asking myself is why are QAbstractItemModel subclasses required to supply a parent() method, and why are they required to supply, in the resulting QModelIndex, the row of a child in the parent?

This requirement forces me to add another layer over my tree data (because I don't want to call indexOf(item) in parent(), it wouldn't be very efficient) that remembers row indexes.

I ask this because it's the first time I see a model based view require this. For example, NSOutlineViewDataSource in Cocoa doesn't require this.

Trolltech devs are smart people, so I'm sure there's a good reason for this, I just want to know what reason.

+1  A: 

The requirement is primarily to support trees. I couldn't tell you the reason, since I'm not a Qt dev... I only use the stuff. However, if you aren't doing trees, you could probably use one of the more-tuned model classes and not have to deal with the overhead of supplying a parent. I believe that both QAbstractListModel and QAbstractTableModel handle the parent portion themselves, leaving you free to just worry about the data you want.

For trees, I suspect that one of the reasons they need the parent is that they try to keep to only asking for the information they need to draw. Without knowing all of the items in a tree (if it wasn't expanded, for example), it becomes much harder to provide an absolute position of a given item in a tree.

As for the quandry of using indexOf(item) in the parent function, have you considered using QModelIndex's internalId or internalPointer? I'm assuming they are available in PyQt... they can be used by your model to track things about the index. You might be able to use that to shortcut the effort of finding the parent's index.

Caleb Huitt - cjhuitt
I know about QAbstractListModel. I need a tree model. I also know about and use internalPointer. However, the reason why I say that I need an extra layer is because in my model, children of tree nodes don't know about their row index (they don't need to know). So even though internalPointer() gives me a pointer to the children instance, I still have to call child.parent.indexOf(child) (even the model in the Qt demo model does this...).What I really want to know if why Qt needs this information when NSOutlineViewDataSource (also a model for a tree view) doesn't.
Virgil Dupras
+3  A: 

The quick answer is, "they thought it best at the time." The Qt developers are people just like you and me -- they aren't perfect and they do make mistakes. They have learned from that experience and the result is in the works in the form of Itemviews-NG.

In their own words from the link above:

Let’s just say that there is room for improvement, lots of room!

By providing a parent that contains a row and column index, they provide one possible way to implement trees and support navigation. They could just as easily have used a more obvious graph implementation.

Kaleb Pederson
Thanks for the blog post link. Interesting to see that everyone can fall in the over-engineering trap.
Virgil Dupras