Results 1 to 7 of 7

Thread: Multiple shortuts for the action

  1. #1
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    23
    Thanks
    3
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:

    Qt Code:
    1. QAction *act1 = new QAction(this);
    2. QAction *act2 = new QAction(act1);
    3. act1->setShortcut("Ctrl+O");
    4. act2->setShortcut("Ctrl+L");
    5. connect(act2, SIGNAL(triggered(bool)), act1, SIGNAL(triggered(bool)));
    6. //...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    23
    Thanks
    3
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:

    Qt Code:
    1. action->setMenuText("Open file\tCtrl+O, Ctrl+L");
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Multiple shortuts for the action

    QKeySequence represents a sequence of keys, which cannot be used as a list of alternatives..

    Workaround:
    Qt Code:
    1. QMenu* menu = menuBar()->addMenu("Menu");
    2. QAction* action = menu->addAction("Action");
    3. action->setShortcut(QKeySequence("Del, Backspace"));
    4. connect(action, SIGNAL(triggered()), this, SLOT(act()));
    5.  
    6. // alternative shortcuts
    7. QAction* del = menu->addAction("");
    8. del->setShortcut(QKeySequence("Del"));
    9. del->setVisible(false);
    10. QAction* bak = menu->addAction("");
    11. bak->setShortcut(QKeySequence("Backspace"));
    12. bak->setVisible(false);
    13. connect(del, SIGNAL(triggered()), action, SLOT(trigger()));
    14. connect(bak, SIGNAL(triggered()), action, SLOT(trigger()));
    15.  
    16. // By the way
    17. // QAction* del = new QAction("", this);
    18. // didn't work even if I set the shortcut context to Qt::ApplicationShortcut.
    19. // At least I couldn't get the actions triggered until I added them in the menu.
    20. // So that's why I added the alternative actions in the menu as hidden..
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    Lemming (4th April 2006)

  7. #6
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    23
    Thanks
    3
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

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

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Multiple shortuts for the action

    I accidentally came up with a better solution:
    Qt Code:
    1. // alternative shortcuts
    2. QShortcut* del = new QShortcut(QKeySequence("Del"), this);
    3. connect(del, SIGNAL(activated()), action, SLOT(trigger()));
    4. QShortcut* bak = new QShortcut(QKeySequence("Backspace"), this);
    5. connect(bak, SIGNAL(activated()), action, SLOT(trigger()));
    To copy to clipboard, switch view to plain text mode 
    So you can forget about the dummy hidden menu items..
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    sshtac (4th March 2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.