使用案例:这应该是一个相当普遍的问题.在正常的QMainWindow中,QMdiArea与QGraphicsView一起生活在mdiChild中.此视图显示内部带有QGraphicsItems的QGraphicsScene.右键单击其中一个项目可选择(聚焦)项目并打开上下文菜单,该菜单可方便地放置在屏幕坐标处QGraphicsSceneMouseEvent::screenPos().这是按预期工作的.
现在,当用户按下某个键(例如Qt :: Key_Menu)时,我想显示相同的上下文菜单.我知道所选的(聚焦的)项目,我知道显示场景的视图.
所以我的问题是:
获取场景中QGraphicsItem的可见表示的位置(在全局,屏幕坐标中)的正确方法是什么?
这是什么不起作用:
QGraphicsItem *item = ...; // is the currently selected item next to which the context menu shall be opened
QGraphicsScene *scene = ...; // is the scene that hosts the item
QGraphicsView *graphicsView = ...; // is the view displaying the scene, this inherits from QWidget
// get the position relative to the scene
QPointF sp = item->scenePos();
// or use
QPointF sp = item->mapToScene(item->pos());
// find the …Run Code Online (Sandbox Code Playgroud)