Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QAbstractItemModel.parent(), why?

Tags:

qt

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.

like image 760
Virgil Dupras Avatar asked May 15 '09 10:05

Virgil Dupras


People also ask

What is QAbstractItemModel?

QAbstractItemModel is called "abstract" for a reason. It does not define or enforce any particular way of storing the model items, it's completely up to you, the developer subclassing QAbstractItemModel and implementing the required interfaces in your subclass.

What is QModelIndex?

The QModelIndex class is used to locate data in a data model.


2 Answers

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.

like image 200
Kaleb Pederson Avatar answered Sep 22 '22 14:09

Kaleb Pederson


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.

like image 32
Caleb Huitt - cjhuitt Avatar answered Sep 18 '22 14:09

Caleb Huitt - cjhuitt