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

  • 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.

  • 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?

  • 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.

  • 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.

  • 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 :)

  • 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);
    QObject::connect(openDir, SIGNAL(triggered()), this, SLOT(OpenDirSelected()));

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.