QAction shortcut does not always work

I have a Qaction in a menu item to delete selected items in one of my views. This 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 am setting up a Delete Key which should trigger the delectAct action. It works most of the time, but in some cases it stops working ... Does anyone know why the shortcut will stop working?

Note: the action still works if I call it from a menu item. Its just a shortcut that doesn't ...

+7
source share
3 answers

You need to add an action to the widget, as it is a widget that will listen for key events. Assuming "this" is basic, just do

 addAction(deleteAct); 

Note that you can add the same action to multiple widgets (which means the whole concept of a split action). So it's good to add it to the main window and to the menu.

+18
source

Try changing the context of the action context , for example:

 deleteAct->setShortcutContext(Qt::ApplicationShortcut); 
+6
source

Without seeing the complete code, I would venture to suggest that somewhere it turns on / off. Make sure that the shortcut gets into the constructor, and not disconnected somewhere else due to the setting.

0
source

All Articles