PyQt:获取 QGraphicsWidgets 在 QGraphicsGridLayout 中的位置
我有一个相当简单的 PyQt 应用程序,其中我将 QGraphicsWidget 的实例放置在 QGraphicsGridLayout 中,并希望将小部件与用 QGraphicsPath 绘制的线连接起来。不幸的是,无论我尝试什么,我总是得到 (0, 0) 作为开始和结束小部件的位置。
我正在使用递归函数构建图形,该函数将小部件添加到场景和布局中。递归函数完成后,布局将添加到新的小部件中,该小部件将添加到场景中以显示所有内容。创建小部件时,边缘将添加到场景中。
如何获得网格布局中任何小部件的非零位置?
更新:我忘了提及我已经在网格布局中的小部件上尝试了 pos() 和 scenePos() 。两者都始终返回 (0, 0) 作为网格中每个小部件的位置。
I have a fairly simple PyQt application in which I'm placing instances of a QGraphicsWidget in a QGraphicsGridLayout and want to connect the widgets with lines drawn with a QGraphicsPath. Unfortunately, no matter what I try, I always get (0, 0) back as the position for both the start and end widgets.
I'm constructing the graph with a recursive function that adds widgets to the scene and layout. Once the recursive function is complete, the layout is added to a new widget, which is added to the scene to show everything. The edges are added to the scene as widgets are created.
How do I get a non-zero position of any of the widgets in the grid layout?
Update: I forgot to mention that I've tried both pos() and scenePos() on the widgets in the grid layout. Both always return (0, 0) as the position for every widget in the grid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
QGraphicsItem::pos()
,它会为您提供项目在父坐标中的位置。当使用 QGraphicsLayout 时,父级可能是包含该对象的单元格,因此坐标等于零。由于您想要将小部件与路径连接起来,因此您需要场景坐标来定义控制点:使用 QGraphicsItem::scenePos() 来获取
QGraphicsWidget
在场景。If you use the
QGraphicsItem::pos()
, it gives you the position of the item in the parent coordinates. When using QGraphicsLayout, the parent is probably the cell containing the object thus the coordinate is equal to zero.Since you want to connect widgets with path, you will need scene coordinate to define the control points: use
QGraphicsItem::scenePos()
to obtain the position of yourQGraphicsWidget
in the scene.