簡體   English   中英

在 QGraphicsScene 上下文菜單中添加 QAction 的快捷方式

[英]Adding a shortcut to QAction inside QGraphicsScene context menu

我的QGraphicsScene子類 WorkspaceScene 包含一個變量,用於稍后在上下文菜單中使用的每個操作。 I have a function that sets up the actions and adds the shortcuts (which is called in the constructor of the class), and then I have a function that creates the context menu, which is called in the contextMenuEvent function I reimplemented.

這是一些相關的代碼:

// Constructor
WorkspaceScene::WorkspaceScene()
{
    _setUpActions();
}

// ContextMenuEvent
void WorkspaceScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    _setUpItemMenu();
    _itemContextMenu.exec(event->screenPos());
}

void WorkspaceScene::_setUpActions()
{
    deleteAction = new QAction("Delete");
    deleteAction->setShortcut(QKeySequence::Delete); // This should add the shortcut
    connect(deleteAction, &QAction::triggered, this, &WorkspaceScene::deleteItemSlot);
}

void WorkspaceScene::_setUpItemMenu()
{
    _itemContextMenu.clear();
    _itemContextMenu.addAction(deleteAction);
}

這成功地設置了操作,它們在上下文菜單中工作,但快捷方式似乎不起作用。 這樣做的正確方法是什么?

我通過將 QAction 添加到 QGraphicsScene 的 QGraphicsView 父級來解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM