QAction shortcut doesnt always work

12,948

Solution 1

You need to add the action to a widget, since it's the widget that will be listening for key events. Assuming "this" is a mainwindow, simply do

addAction(deleteAct);

Note that you can add the same action to multiple widgets (that's the whole point of the separated action concept). So it's fine to add it to the mainwindow and to a menu.

Solution 2

The shortcut works depending on the focus of the application views.
I wanted to have shortcuts working on buttons.
In my application I changed the shortcut context of the action,
added the action to the widget
and finally to the subviews of the application.
Then the necessary signals and slots of widget an action must be connected.

const QAbstractButton*button  = dynamic_cast<QAbstractButton*>(widget);

action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
widget->addAction(action);
ui->textBrowser->addAction(action);
ui->treeSource->addAction(action);

if (button)
{
    if (button->isCheckable())
    {
        action->setCheckable(true);
        if (button->isChecked()) action->setChecked(true);
        connect(action, SIGNAL(triggered(bool)), button, SLOT(setChecked(bool)));
        connect(button, SIGNAL(clicked(bool)), action, SLOT(setChecked(bool)));
    }
    else
    {
        connect(action, SIGNAL(triggered()), button, SLOT(click()));
    }
}
Share:
12,948
MBU
Author by

MBU

Updated on June 03, 2022

Comments

  • MBU
    MBU almost 2 years

    I have a Qaction on a menu item for deleting selected items in one of my views. Here is how i create the action:

    deleteAct = new QAction( tr("Delete Selected"), this);
    deleteAct->setShortcut(QKeySequence::Delete);
    connect(deleteAct, SIGNAL(triggered()), this, SLOT(deleteSelected()));  
    

    I setup a keyboard shortcut (Delete Key) which should trigger the delectAct action. It works most of the time but at some points it stops working... Does anyone know why the shortcut would stop working?

    Note: the action still works if i trigger it from the menu item. Its just the shortcut that doesn't...

  • rendon
    rendon over 11 years
    +1 My context menu shortcuts didn't work as I wanted until I have added to the main window actions too.
  • Tob
    Tob almost 8 years
    In addition, action->setShortcutContext( ... ); has to be set, see user362638 answer.
  • David Faure
    David Faure almost 8 years
    Not necessarily; the default shortcut context is Qt::WindowShortcut, which is what you want in the example of the original poster. You don't want the Delete key to delete the selected item in window A when pressing Delete in window B, surely. Qt::ApplicationShortcut makes sense for application-wide shortcuts (i.e. shortcuts that should work from any window of the application).
  • bhaller
    bhaller over 4 years
    I just had a case where the shortcut for a menu item in a submenu of the main menu bar of my app wasn't working half the time, seemingly randomly. Since the action was tied to a menu item, it seems like it ought to have been triggered reliably (and the menu item definitely was not being disabled, etc.). Nevertheless, adding the action to the mainwindow of my app, as recommended here, fixed the problem; the shortcut now triggers reliably. I had already done setShortcutContext(Qt::ApplicationShortcut), so that was not the issue. Seemed like a Qt bug, but anyway, this fixed it.