PDA

View Full Version : Multiple shortuts for the action



Lemming
3rd April 2006, 20:32
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

wysota
3rd April 2006, 20:54
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:


QAction *act1 = new QAction(this);
QAction *act2 = new QAction(act1);
act1->setShortcut("Ctrl+O");
act2->setShortcut("Ctrl+L");
connect(act2, SIGNAL(triggered(bool)), act1, SIGNAL(triggered(bool)));
//...

Lemming
3rd April 2006, 23:09
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?

wysota
3rd April 2006, 23:36
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:


action->setMenuText("Open file\tCtrl+O, Ctrl+L");

jpn
4th April 2006, 08:58
QKeySequence represents a sequence of keys, which cannot be used as a list of alternatives..

Workaround:


QMenu* menu = menuBar()->addMenu("Menu");
QAction* action = menu->addAction("Action");
action->setShortcut(QKeySequence("Del, Backspace"));
connect(action, SIGNAL(triggered()), this, SLOT(act()));

// alternative shortcuts
QAction* del = menu->addAction("");
del->setShortcut(QKeySequence("Del"));
del->setVisible(false);
QAction* bak = menu->addAction("");
bak->setShortcut(QKeySequence("Backspace"));
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..

Lemming
4th April 2006, 13:55
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.

jpn
6th April 2006, 22:29
I accidentally came up with a better solution:


// alternative shortcuts
QShortcut* del = new QShortcut(QKeySequence("Del"), this);
connect(del, SIGNAL(activated()), action, SLOT(trigger()));
QShortcut* bak = new QShortcut(QKeySequence("Backspace"), this);
connect(bak, SIGNAL(activated()), action, SLOT(trigger()));

So you can forget about the dummy hidden menu items..