Has anyone been able to attach an standard QWidget to a QGraphicsItem, say a QSpinBox to modify a float value that the QGraphicsItem is representing?
I know it is possible to add a QWidget to a QGraphicsScene but I want to attach it to an Item in my Scene, rather than the scene itself.
I’m not sure QGraphicsItem supports that directly unless you paint it in yourself. I think what you’re looking for if you want to display widgets is an inheritor called QGraphicsWidget. Then you can just add it as a child of your custom QGraphicsItem. QGraphicsWidget Class | Qt Widgets 5.15.17
There doesn’t seem to be any method on the QGraphicsWidget to add a standard widget as a child.
My current plan/work around is to add a standard widget to the scene, overlaying it on top of the QGraphicsItem/Widget and then use a custom moving signal to keep the standard widget aligned to the Graphics widget if/when it is moved.
Here’s a proof of concept, but probably not the most efficient. You need to use a special layout reserved for GraphicsWidget items. I inherited in this example, but the documentation proposes composition if you’re simply adding widgets to a scene. More importantly is that widgets need to be added to the scene despite all other requirements. Note that I parented the QGraphicsWidget to a QGraphicsRectItem and it works as intended.