[PyKDE] Model indexes and internal pointers

Arve Knudsen arve.knudsen at gmail.com
Wed Jul 19 07:43:26 BST 2006


Andreas' explanation sounds correct to me, it's obvious that one
should be able to access data stored with an index created in the
index() method when it is later passed to methods such as data().

What happened in my case was that I created indexes in index() with
references to Python objects that turned out to have no other
references in Python, and so when I later tried to access those
objects in e.g. data() they were destroyed.

Arve

On 7/19/06, Andreas Pakulat <apaku at gmx.de> wrote:
> On 19.07.06 00:53:42, David Boddie wrote:
> > On Tue, 18 Jul 2006 10:35:17 +0200, Arve Knudsen wrote:
> > > When you say that model indexes are temporary, do you mean that if one
> > > keeps a reference to a wrapping Python object, the underlying C++
> > > object may still get destroyed? That doesn't apply to my case at
> > > least, I only need to access indexes' associated data in reimplemented
> > > model methods (such as data()).
> >
> > What I mean is that you should create/obtain them and pass them to the
> > component view/model that requires them, but not store them for later
> > use.
> >
> > http://doc.trolltech.com/4.1/model-view-model.html#model-indexes
> >
> > As far as I understand it, the model indexes you create in a model's
> > index() method will be transferred to C++ and destroyed. If, for some
> > reason, you receive a model index later and try to access the object
> > referenced in its internalPointer() method then you'll run into trouble.
>
> In the context of PyQt4 and not saving a reference to the object
> yourself, you are right. But if you have such a reference or in the case
> of the C++ binding of Qt your wrong, if you create a model index with an
> internalPointer pointing to "something", this index will be later passed
> onto methods such as data(). Those can safely assume that if
> internalPointer points to something, it is an object that they can work
> on and not some random memory.
>
> > > Anyway, I think that models and views are so integral to Qt
> > > programming now that some robustness towards passing data between them
> > > should definitely be considered.
> >
> > Probably, but there's not much you can do if you attach Python objects
> > to temporary C++ objects and pass them back to Qt. If you just prevent
> > them from being collected, there's the chance that they'll become
> > unrecoverable. Maybe the handling of QPersistentModelIndex needs to be
> > looked at more closely.
>
> The C++ model indexes are not so temporary as you might think. The Qt
> views cache the indexes for alter use (which is one of the reasones, why
> you need to emit signals like layoutChanged or modelReset). Also this
> stuff works pretty well in C++ world, i.e. creating an object inside
> index() function on the heap, passing it to the createIndex() method and
> later on when data is called with an index of the same row, column and
> parent as was created in the index() method, you can count on it having
> an internalPointer pointing to the object on the heap that you created.
>
> Andreas
>
> --
> Don't let your mind wander -- it's too little to be let out alone.
>
> _______________________________________________
> PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
> http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
>




More information about the PyQt mailing list