[PyQt] Problem with implementation of dummy (no-op) proxy model

Hans Meine meine at informatik.uni-hamburg.de
Thu Oct 28 15:51:49 BST 2010


Hi everybody, hi Phil,

Op den Dunnersdag 28 Oktober 2010 Klock 14:28:16 hett Hans Meine schreven:
> I am about to give up now, but first I want to post my current code again;
> I added some assertions which show an interesting fact which I have no
> explanation for:  One assertion fails, since I am getting a QModelIndex of
> my proxy model in which internalPointer does *not* contain a QModelIndex
> of the proxied model anymore, but NULL/None.
> 
> This is very strange, and with persistent QMIs, it crashes completely.

(I think) I now have a good explanation for this, and I would propose to add 
something like this to the docs, i.e. to
  http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html

> Maybe it is a bug/limitation of PyQt, and QMI's internalPointer() does not
> increment the refcount?  That would explain a lot...

This is exactly the case:

Since QModelIndex does not derive from QObject, and it just carries a void* as 
internalPointer(), it is not possible to offer proper reference counting in 
PyQt.  Any pointer that would directly or indirectly be installed in the 
internalPointer() could be copied or deleted an uncontrollable number of times 
on the C++ library side, without PyQt having any chance to notice.

That means, that *one must only pass objects to createIndex() whose lifetime 
is longer than that of the model indices*.  This is completely independent of 
the use of QPersistentModelIndex or any particular model subclasses offered by 
Qt; as long as you pass references to objects to createIndex() and later 
access them via internalPointer(), you need to make sure that they are not 
garbage collected in the meantime.  (A common example would be to pass e.g. 
QObjects with a parent in an existing object hierarchy.)

Hoping that this helps others coming after me,
  Hans


More information about the PyQt mailing list