Multiple inheritance for QGraphicsItem

Maurizio Berti maurizio.berti at gmail.com
Mon Jun 10 04:39:54 BST 2024


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

-- 
È 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/20240610/d9f017d8/attachment.htm>


More information about the PyQt mailing list