Results 1 to 6 of 6

Thread: Additional keyboard shortcut for a menu

  1. #1
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Additional keyboard shortcut for a menu

    The first item in my main menu is called "Project":

    Qt Code:
    1. m_projectMenu = new QMenu("Pro&ject", m_menuBar);
    To copy to clipboard, switch view to plain text mode 

    So the user can hit "Alt+J" to open the menu via keyboard shortcut. Great!

    For historical reasons, I'd also like the keyboard shortcut "Alt+F" to open the menu. I thought I could use a QShortcut, and hook it up to the menu's action:

    Qt Code:
    1. QShortcut* fileshortcut = new QShortcut(QKeySequence("Alt+F"), m_projectMenu);
    2. QObject::connect(fileshortcut, SIGNAL(activated()),
    3. m_projectMenu->menuAction(), SLOT(trigger()));
    To copy to clipboard, switch view to plain text mode 

    But then pressing "Alt+F" only acts as if the mouse is hovering over the menu, rather than clicking to display the menu.

    Then I thought of adding multiple keyboard shortcuts to the menu:

    Qt Code:
    1. QList<QKeySequence> shortcuts;
    2. shortcuts.push_back(QKeySequence("Alt+F"));
    3. shortcuts.push_back(QKeySequence("Alt+J"));
    4.  
    5. m_projectMenu->menuAction()->setShortcuts(shortcuts);
    To copy to clipboard, switch view to plain text mode 

    But that doesn't work either. "Alt+F" does nothing, and "Alt+J" results in messages in stdout/stderr:

    QAction::eventFilter: Ambiguous shortcut overload: Alt+J

    So what's the correct way to do this? It seems that menuAction() does not return what I thought it returned, but the docs only say "Returns the action associated with this menu." Is that not the action on which keyboard shortcuts should be installed?

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Additional keyboard shortcut for a menu

    You can do this like :

    Qt Code:
    1. QShortcut* fileshortcut = new QShortcut(QKeySequence("Alt+F"), this);
    2. QObject::connect(fileshortcut,SIGNAL(activated()),this,SLOT(customSlot()));
    3.  
    4. void customSlot()
    5. {m_projectMenu->exec();}
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to yogeshgokul for this useful post:

    negritot (15th September 2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Additional keyboard shortcut for a menu

    Thank you! That gets me one step of the way.

    The next step is to display the menu in the correct location (using exec with no parameters shows the menu at location 0,0 in global screen coordinates). The documentation suggests that I can display the menu aligned with a specific widget by getting the widget's global coordinates using QWidget::mapToGlobal:

    Qt Code:
    1. void UIControl::openProjectMenu()
    2. {
    3. QPoint menuPos = m_projectMenu->mapToGlobal(QPoint(0,0));
    4. m_projectMenu->exec(menuPos);
    5. }
    To copy to clipboard, switch view to plain text mode 
    However, that still displays the menu at location 0,0. I believe that I'm not using the correct widget in the mapToGlobal call, but I'm not sure what other widget to use. Any ideas?

  5. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Additional keyboard shortcut for a menu

    Here is your solution.

    Qt Code:
    1. void UIControl::openProjectMenu()
    2. {
    3. QPoint p = menubar->mapToGlobal(menubar->pos());
    4. p.setY(p.y() + menubar->height());
    5. m_projectMenu->exec(p);
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    negritot (16th September 2009)

  7. #5
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Additional keyboard shortcut for a menu

    yogeshgokul, thanks so much for the solution!

    I'm curious: what would you do if you needed to position the menu under the second menu entry in the menu bar?

  8. #6
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Additional keyboard shortcut for a menu

    Quote Originally Posted by negritot View Post
    I'm curious: what would you do if you needed to position the menu under the second menu entry in the menu bar?
    I knew that you will arise this point. Ok !
    So the solution is not straight forward for that. You can do it by playing with size of entries in menubar.
    But I suggest you to do not waste time for this thing. Go straight and use default hot key for menus forget shortcuts for menu invoking.
    Lets use shortcuts for QActions only.

Similar Threads

  1. Invoke QCompletion on keyboard shortcut
    By aspidites in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2009, 10:32
  2. Rerouting messages for keyboard shortcut selection dialog.
    By andy.fillebrown in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:55
  3. Shortcut key for context menu
    By darshan.hardas in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:32
  4. Button shortcut call an element in the menu
    By mourad in forum Qt Programming
    Replies: 5
    Last Post: 29th May 2008, 10:02

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.