[PyQt] Questions about Trees

David Boddie david at boddie.org.uk
Fri Oct 16 00:51:17 BST 2009


On Thu, 15 Oct 2009 01:32:57 -0700 (PDT), William wrote:

> Hi!  I've been trying to fight my way though the model-view-delegate
> paradigm in pyqt and must admit that I'm a bit confused.  Tables weren't
> that bad, but now I've reached trees....So, I have some questions.  My
> first question is what are the reasons to use internalptr as compared to
> internalid.

>From memory, I think that it just makes it simpler to write a model if you
pass objects to createIndex() and use the internalPointer() method to
retrieve them again later.

The internalId() approach may require you to store items that you create
in a dictionary somewhere, which involves an extra lookup later on, but
this isn't the real problem. Basically, the model/view API doesn't give
you any way of knowing when it has finished with any given item, so you
will end up with a dictionary containing all the items that you have issued
to the view, which is possibly a bit wasteful.

At least, that is how I seem to recall it going. Other than some very
recent attempts to debug and write models, it's been a while since I looked
at model/view stuff seriously.

[...]

> Is there a reason to employ one approach as compared to the other?

You might use internalId() if you have a way of mapping integers to data
that doesn't require the creation of a tree of items. Maybe you have some
kind of database behind the model class that uniquely associates integers
with data.

I seem to remember some interesting tricks with bit masking where you could
model the position of items in a hierarchy by splitting up a 32 bit integer
into different fields. These kinds of tricks might work quite well for
specialised models.

David



More information about the PyQt mailing list