[PyKDE] Model indexes and internal pointers

David Boddie david at boddie.org.uk
Tue Jul 18 23:53:42 BST 2006


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.

Having said that, it would probably mean that the view had kept those
indexes around, maybe as persistent indexes, and that might be why you
received indexes with internal pointers that used to be valid, but had
become out of date when the Python objects were collected.

It sounds like you are doing the right thing, anyway, though it would be
interesting to know where the segfaults were occurring in your application.

> 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.

David




More information about the PyQt mailing list