13

用例:这应该是一个相当普遍的问题。在带有 QMdiArea 的普通 QMainWindow 中,有一个带有 QGraphicsView 的 mdiChild。这个视图显示了一个 QGraphicsScene,里面有 QGraphicsItems。右键单击这些项目之一选择(聚焦)该项目并打开上下文菜单,该菜单方便地放置在屏幕坐标处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 global (screen) position of the item
QPoint global = graphicsView->mapToGlobal(graphicsView->mapFromScene(sp));

// now
myContextMenu.exec(global);
// should open the context menu at the top left corner of the QGraphicsItem item, but it goes anywhere

文档说: 如果你想知道一个项目在视口中的位置,你可以在项目上调用 QGraphicsItem::mapToScene() ,然后在视图上调用 QGraphicsView::mapFromScene() 。
这正是我正在做的,对吧?


刚刚偶然发现德国论坛上的一个帖子暗示:

QGraphicsView *view = item->scene()->views().last();

甚至更好:

QGraphicsView *view;
foreach (view,  this->scene()->views())
{
    if (view->underMouse() || view->hasFocus()) break;
}
// (use case in the forum thread:) // QMenu *menu = new QMenu(view);

使用它可能会更普遍地回答我的问题......

4

3 回答 3

9

我找到了一个可行的解决方案。
QGraphicsItem 必须在屏幕上可见。(可能如果它不可见,因为视图显示了场景的其他点,则可以将该点限制在视图视口的矩形上。)

// get the screen position of a QGraphicsItem
// assumption: the scene is displayed in only one view or the first view is the one to determine the screen position for
QPoint sendMenuEventPos; // in this case: find the screen position to display a context menu at
QGraphicsItem *pFocusItem = scene()->focusItem();

if(scene() != NULL // the focus item belongs to a scene
    && !scene()->views().isEmpty() // that scene is displayed in a view...
    && scene()->views().first() != NULL // ... which is not null...
    && scene()->views().first()->viewport() != NULL // ... and has a viewport
    )
{
    QGraphicsView *v = scene()->views().first();
    QPointF sceneP = pFocusItem->mapToScene(pFocusItem->boundingRect().bottomRight());
    QPoint viewP = v->mapFromScene(sceneP);
    sendMenuEventPos = v->viewport()->mapToGlobal(viewP);
}

if(sendMenuEventPos != QPoint())
{
    // display the menu:
    QMenu m;
    m.exec(sendMenuEventPos);
}

使用视图的视口将视图坐标映射到全局坐标非常重要。

上下文菜单键(Qt::Key_Menu)的检测发生在keyPressEvent()“主”QGraphicsItem 中(由于我的程序结构)。

于 2013-04-04T10:45:16.467 回答
1

代码似乎是正确的。但是上下文菜单的创建可能存在一些问题。

您是否将 QContextMenu 的父级设置为 MainWindow (或您的应用程序中的那种)?

我认为这可能是你的问题。

祝你好运!!

于 2012-03-26T18:17:34.240 回答
1

只是在黑暗中刺伤,但看看这个http://www.qtcentre.org/threads/36992-Keyboard-shortcut-event-not-received

在查看 Qt 文档时,似乎使用 QGraphicsView 可能会导致一些关于快捷方式的异常行为。

看起来好像有一种规范的方式可以实现你想要的结果。

根据您实现上下文菜单、快捷方式和 QGraphicsView 的方式,您可能需要适当地为 QGraphicsView 设置 Qt::ContextMenuPolicy 并以不同的方式构建和调用菜单。

我对这个问题很感兴趣,因为我很快需要做一些非常相似的事情!

于 2012-03-27T10:40:12.537 回答