[PyQt] Converting C++ to Python

Phil Thompson phil at riverbankcomputing.co.uk
Thu Sep 20 18:05:14 BST 2007

On Thursday 20 September 2007, Rajeev J Sebastian wrote:
> Hello Phil,
> On 9/20/07, Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
> > > The problem is that the first element *should* actually be a
> > > <__main__.DiagramTextItem> rather than a QGraphicsItem.
> > > (DiagramTextItem is a subclass of QGraphicsTextItem).
> > >
> > > In other words, for some reason, python subclasses of
> > > QGraphicsTextItem arent recognized correctly when the same are
> > > returned from C++.
> >
> > No.
> >
> > Is your DiagramTextItem reimplementing type()?
> >
> > I'm guessing it is. In which case PyQt isn't recognising it and falling
> > back to QGraphicsItem. If you don't reimplement type() it should at least
> > recognise it as a QGraphicsTextItem. If this isn't good enough then you
> > will have to keep a reference (probably in a list) of each
> > DiagramTextItem instance so that items() will see that the C++ addresses
> > it is getting back to Qt correspond to existing Python objects (of the
> > correct type).
> Thank you for your quick response.
> Yes, in fact I am reimplementing type() in DiagramTextItem.
> I would like to point out that DiagramItem and Arrow are both
> subclasses of QGraphicsItem (QGraphicsPolygonItem and
> QGraphicsLineItem respectively), and both also reimplement type().
> However, PyQt4 seems to recognize them properly and returns objects
> with the right type.

Yes, PyQt should be keeping that extra reference for you and so everything 
should work.

> Hence, my question on whether the special aspect of QGraphicsTextItem
> is affecting this code.

Hmm - one possibility might be that the cast is returning a different address 
which then isn't being recognised. Can you send me (off-list) a copy of what 
you've got so far and I'll take a proper look.

> I tried to do what you had suggested, i.e., store references to these
> objects. However, that doesnt work as expected. I still get
> QGraphicsItem objects.

Yes, forget I said that - it shouldn't be necessary.


More information about the PyQt mailing list