Multiple inheritance for QGraphicsItem

Maurizio Berti maurizio.berti at gmail.com
Wed Jun 12 17:40:50 BST 2024


Hi Phil,
Ok, I see. It's a pity, but I understand your reasons.
At least, there are the options I listed in my first message, I just need
to be careful in their implementation.

Thanks anyway.
Best regards,
MaurizioB

Il giorno lun 10 giu 2024 alle ore 16:45 Phil Thompson <
phil at riverbankcomputing.com> ha scritto:

> On 10/06/2024 04:39, Maurizio Berti wrote:
> > As we all know, PyQt doesn't allow multiple inheritance of more Qt
> > classes,
> > except when explicitly allowed (eg. QTextObjectInterface).
> >
> > When using the Graphics View framework, though, it is sometimes useful
> > to
> > have a QGraphicsItem subclass that also provides QObject features: for
> > example, a QGraphicsRectItem that emits a signal whenever its geometry
> > changes.
> >
> > Right now, whenever I need something similar, I normally choose one of
> > the
> > following options, depending on my needs and code simplicity:
> >
> > - create a dummy QObject subclass and use an instance of it as a member
> > of
> > the actual QGraphicsItem one, then eventually emit its signals whenever
> > necessary;
> > - create a dummy QGraphicsObject subclass as the actual one to be used,
> > with the "real" items as its children, then implementing boundingRect()
> > (possibly returning childrenBoundingRect()) and a no-op paint(), and
> > eventually "copying" the actual child item common functions by proper
> > implementation, simple monkey patching or even __getattr__ override;
> >
> > While both solutions "work", they are not really acceptable, nor robust
> > or
> > reliable:
> >
> > - the dummy QObject "proxy" is an unnecessary difficulty that
> > complicates
> > access to the actual signal source; using Qt properties is a further
> > complication (consider the case of using QPropertyAnimation);
> > - the "main QGraphicsObject" approach adds complexity and further
> > difficulties in geometry/transform management and access to the
> > actually
> > intended item;
> > - issues with the lifespan of the QObject and the "real" QGraphicsItem,
> > including connected signals (eg. using QTimers or animations);
> >
> > Qt actually considers and allows such multiple inheritance (it's the
> > very
> > nature of QGraphicsObject).
> >
> > So, my question is quite simple: would it be possible to allow proper
> > multiple inheritance for QGraphicsItem+QObject?
> > Therefore, would that implicitly allow multiple inheritance for all
> > QGraphicsItem inherited classes (the standard ones and those created in
> > Python, including mixins)?
> >
> > Thanks,
> > MaurizioB
>
> It's technically possible by creating a C++ class derived from QObject
> and QGraphicsRectItem and wrapping that (and the same for every other
> QGraphicsItem), but I'm not going to. I'm not going to start creating a
> higher-lever API above Qt.
>
> Phil
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240612/11b550d9/attachment.htm>


More information about the PyQt mailing list