Multiple shortuts for the action
I want one of my menu actions to have multiple shortcuts (like "Del" and "Backspace"), so the user can press any of those keys to activate the action.
Using myAction->setShortcut( QKeySequence( "Del, Backspace" ) ) doesn't give a desired result - I have to press those keys in combination instead of pressing any of them.
Is it possible to assign multiple shortcuts for one action with QT?. If possible - how?
Thanks in advance
Re: Multiple shortuts for the action
You can have two or more actions connected to each other with each action having different key binding and triggering the "main" actions signals.
Something like:
Code:
act1->setShortcut("Ctrl+O");
act2->setShortcut("Ctrl+L");
connect(act2, SIGNAL(triggered(bool)), act1, SIGNAL(triggered(bool)));
//...
Re: Multiple shortuts for the action
Thank you for this workaround. But it doesn't solve the problem completely: it would be nice to have both shortcuts visible on the menu action and the mentioned approach will show only one shortcut per action.
Does somebody know whether QT offers some facilities for this?
Re: Multiple shortuts for the action
You can change the text Qt displays as the shortcut in the menu. At least it was possible in Qt3. To do that (AFAIR) you had to use tab with your menu text:
Code:
action->setMenuText("Open file\tCtrl+O, Ctrl+L");
Re: Multiple shortuts for the action
QKeySequence represents a sequence of keys, which cannot be used as a list of alternatives..
Workaround:
Code:
QMenu* menu
= menuBar
()->addMenu
("Menu");
QAction* action
= menu
->addAction
("Action");
connect(action, SIGNAL(triggered()), this, SLOT(act()));
// alternative shortcuts
QAction* del
= menu
->addAction
("");
del->setVisible(false);
QAction* bak
= menu
->addAction
("");
bak->setVisible(false);
connect(del, SIGNAL(triggered()), action, SLOT(trigger()));
connect(bak, SIGNAL(triggered()), action, SLOT(trigger()));
// By the way
// QAction* del = new QAction("", this);
// didn't work even if I set the shortcut context to Qt::ApplicationShortcut.
// At least I couldn't get the actions triggered until I added them in the menu.
// So that's why I added the alternative actions in the menu as hidden..
Re: Multiple shortuts for the action
Quote:
Originally Posted by jpn
QKeySequence represents a sequence of keys, which cannot be used as a list of alternatives..
That's what I was afraid of. Looks like I'll have to implement the workaround with two actions.
Thank you.
Re: Multiple shortuts for the action
I accidentally came up with a better solution:
Code:
// alternative shortcuts
connect(del, SIGNAL(activated()), action, SLOT(trigger()));
connect(bak, SIGNAL(activated()), action, SLOT(trigger()));
So you can forget about the dummy hidden menu items..