Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved Adding a shortcut to QAction inside QGraphicsScene context menu

    General and Desktop
    2
    6
    168
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      Forfunckle last edited by

      My QGraphicsScene subclass WorkspaceScene contains a variable for each action that it later uses inside the context menu. 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.

      Here's some relevant code:

      // 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);
      }
      

      This successfully sets up the actions and they work inside the context menu but the shortcut doesn't seem to work. What's the correct way of doing this?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        In that case you need to also add the action to the widget.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        F 1 Reply Last reply Reply Quote 1
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Do you mean you want the action to be triggered by the delete key when the menu is opened ? Or also the rest of the time ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          F 1 Reply Last reply Reply Quote 0
          • F
            Forfunckle last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • F
              Forfunckle @SGaist last edited by

              @SGaist I'd like the action to be triggerred at any point (even if the menu is not opened)

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                In that case you need to also add the action to the widget.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                F 1 Reply Last reply Reply Quote 1
                • F
                  Forfunckle @SGaist last edited by

                  @SGaist That made it work. Thanks!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post