[PyQt] QModelIndex.internalPointer

Hans-Peter Jansen hpj at urpla.net
Mon Apr 25 11:23:48 BST 2011


On Monday 25 April 2011, 11: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.

You miss to note your OS/Python/Qt/sip/PyQt versions in use. 

Phil fixed some related issues in one of the latest sip/PyQt releases 
(hint, hint..). 

Pete



More information about the PyQt mailing list