[PyQt] QModelIndex.internalPointer

Andreas Pakulat apaku at gmx.de
Mon Apr 25 16:15:31 BST 2011


On 25.04.11 10:05:17, Jeremy Sanders wrote:
> Hi - I've hit several crashes with Qt's model code with PyQt.
> 
> Can someone clear up the ownership rules for objects given to 
> QAbstractItemModel.createIndex and accessed with 
> QModelIndex.internalPointer?
> 
> I tried creating a layer of indirection when using createIndex, e.g.
> 
> class Indirect(object):
>   def __init__(self, realobj):
>     self.realobj = realobj
> 
> ...
> self.createIndex(row, col, Indirect(realobj))
> 
> This breaks rather horribly. The objects returned by internalPointer can be 
> completely random (the model index is however valid), with a different type. 
> Is because PyQt uses some sort of python id, which is reallocated by python 
> because the Indirector has no references to it?
> 
> I suppose I will have to hack something like storing a reference to the 
> Indirect object in the realobj.

I haven't used internalPointer since a long time with PyQt, so things
may have changed a lot by now. But back in the earlier days of PyQt the
application indeed was required to keep a reference itself to the
objects that are passed to the createIndex function that takes a
pointer. This matches the C++ behaviour, where the model index also does
not take ownership of the object.

So it might make more sense use internalIndex with a list and have the
list store your Indirect instances...

Andreas
 


More information about the PyQt mailing list