[SOLVED] Action shortcut not triggering unless action is placed in a toolbar
-
wrote on 19 Mar 2012, 16:28 last edited by
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.
-
wrote on 19 Mar 2012, 20:00 last edited by
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?
-
wrote on 20 Mar 2012, 15:58 last edited by
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.
-
wrote on 21 Mar 2012, 09:58 last edited by
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.
-
wrote on 21 Mar 2012, 16:04 last edited by
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 :)
-
wrote on 23 Nov 2013, 19:00 last edited by
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);@