Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding a shortcut to QAction inside QGraphicsScene context menu

Adding a shortcut to QAction inside QGraphicsScene context menu

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 548 Views
  • 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 Offline
    F Offline
    Forfunckle
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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
        0
        • F Offline
          F Offline
          Forfunckle
          wrote on last edited by
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS SGaist

            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 ?

            F Offline
            F Offline
            Forfunckle
            wrote on last edited by
            #4

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              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
              1
              • SGaistS SGaist

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

                F Offline
                F Offline
                Forfunckle
                wrote on last edited by
                #6

                @SGaist That made it work. Thanks!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved