[PyQt] Subclass of QGraphicsObject does not have the correct class name listed in QGraphicsScene.items()

Luke Campagnola lcampagn at email.unc.edu
Mon Dec 5 17:24:57 GMT 2011


Howdy Phil,
I'm running into this issue where QGraphicsScene.items() does not
return the correct python objects if the items are subclassed from
QGraphicsObject. Your response to this issue several months ago was:

On Fri, May 20, 2011 at 11:46, Phil Thompson
<phil at riverbankcomputing.com> wrote:
> It's because QGraphicsObject inherits both QObject and QGraphicsItem.
> items() returns a list of QGraphicsItems which, for a QGraphicsObject, has
> a different C++ address than the original QGraphicsObject. PyQt doesn't
> recognise that the QGraphicsItem is a cast of the QGraphicsObject. I don't
> think there is anything I can (sensibly) do about this.

The workaround I am using for this bug is to maintain a dictionary
that maps from the QtGui::QGraphicsItem memory address back to the
original python object. Looks something like:
    cache[ sip.unwrapinstance(sip.cast(item, QtGui.QGraphicsItem)) ] = item

This works, but it's rather messy since every instance of
QGraphicsObject and QGraphicsWidget needs to register itself with this
cache. I presume PyQt already maintains a similar dictionary so that
it can translate between Qt's internal memory addresses and PyQt's
wrapper objects. Would it not be straightforward to implement my
workaround from within PyQt?

Luke


More information about the PyQt mailing list