Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QAction shortcut doesnt always work

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

like image 527
MBU Avatar asked Feb 16 '12 21:02

MBU


People also ask

Why sometimes shortcut keys are not working?

Keyboard shortcuts could fail to work if you don't press the key combinations at the same time. If you are having difficulties pressing multiple keys at the same time, you should enable Sticky Keys. This Windows 10 feature allows you to press the shortcut keys one after another.


1 Answers

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.

like image 101
David Faure Avatar answered Sep 20 '22 15:09

David Faure