PyQt: Get QGraphicsWidgets Position in QGraphicsGridLayout

I have a fairly simple PyQt application in which I place QGraphicsWidget instances in a QGraphicsGridLayout and want to connect widgets with lines drawn using QGraphicsPath. Unfortunately, no matter what I try, I always get (0, 0) back as a position for both the start and end widgets.

I am building a graph with a recursive function that adds widgets to the scene and layout. Once the recursive function is completed, the layout is added to the new widget, which is added to the scene to show everything. Edges are added to the scene as widgets are created.

How to get non-zero position of any of the widgets in the grid layout?

Update: I forgot to mention that I tried both pos () and scenePos () in widgets in the grid layout. Both always return (0, 0) as the position for each widget in the grid.

+3
source share
1 answer

If you use QGraphicsItem::pos(), it gives you the position of the element in parent coordinates. When using QGraphicsLayout, the parent is probably the cell containing the object, so the coordinate is zero.

Since you want to connect widgets using the path, you will need the scene coordinate to determine the control points: use QGraphicsItem::scenePos()to get your position QGraphicsWidgetin the scene.

+1
source

Source: https://habr.com/ru/post/1744716/


All Articles