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. [SOLVED] Action shortcut not triggering unless action is placed in a toolbar
QtWS25 Last Chance

[SOLVED] Action shortcut not triggering unless action is placed in a toolbar

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 18.5k 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.
  • N Offline
    N Offline
    notgary
    wrote on last edited by
    #1

    Hi there,

    I have a number of actions placed within a QMenu, some of which have shortcuts associated with them. The class from which they are created isn't QAction, rather it's a custom class that derives directly from QAction. The problem I'm facing is that the shortcuts don't seem to work unless the actions placed within a toolbar, ie are visibible. If they are placed in the menu, ie not visible, then the shortcut does not work.

    I've tried using QAction::setShortcutContext to alter the whatever the behaviour is being set as, but that hasn't helped. Does anyone know what might be happening here to cause the shortcut to be ignored?

    Thanks in advance.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      There is usually no need to bind a QAction to a specific widget to enable shortcuts. It is sufficient to bind them to any active widget (for example the main window itself) at least once.

      Can you show us some code?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        notgary
        wrote on last edited by
        #3

        Hi Lukas,

        Thanks a lot for your reply. Unfortunatley, I am unable to provide any actual source code as it is part of a commercial application, however I can provide a few more details that I've just learned today.

        The QMenu in question isn't being placed on the menuBar of the QMainWindow, rather it is a member of Windows-style start menu that derives from QAbstract button, which is menually painted onto the corner of the window. It's clicking on this button that reveals the menu.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          A shortcut should be triggered if it has a valid parent widget and this parent widget receives events (id est it is not hidden). In addition, if the shortcut context is Qt::WindowShortcut (default) the widget has to be active.

          This means for QAction, that it has to be added to at least one visible widget. If your menu is hidden per default the easiest solution is to add the actions to the main window itself or your QAbstractButton subclass.

          If you don't want to do so, for example because you use the main window actions to populate its context menu, you may add a zero-sized widget to the main window layout which has all the actions added. If you want to have some automatism override the actionEvent in your QAbstractButton subclass and add / remove actions to the zero-sized widget or the menus accordingly.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            notgary
            wrote on last edited by
            #5

            Hi Lukas,

            Thanks a lot for your help. I took your advice and added the actions to the QAbstractButton as well as the menu and that solved the problem.

            Thanks a lot :)

            1 Reply Last reply
            0
            • B Offline
              B Offline
              big_buka
              wrote on last edited by
              #6

              Hi Lukas,
              I have the simula problem.
              I have a ListView with custom QAbstractListModel and handeled slot customContextMenuRequested in it.
              I've tryed to add actions to ListView itself but this don't give any result
              listView is visible but actions are not triggered
              Here is some code
              @openDir = new QAction(QIcon(":/MenuIcons/open-folder"),tr("ACTION_OPEN_FOLDER"),listView);
              openDir->setObjectName("ACTION_OPEN_DIR");
              QObject::connect(openDir, SIGNAL(triggered()), this, SLOT(OpenDirSelected()));
              openDir->setShortcut(QKeySequence("Enter"));
              menu->addAction(openDir);
              listView->addAction(openDir);@

              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