Results 1 to 7 of 7

Thread: Same shortcut for different actions in different widgets

  1. #1
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Same shortcut for different actions in different widgets

    Hi, my application has some actions with a shortcut associated to them which are accessible through my application's menu and various toolbars. My application also has some dockable windows with their own actions, some of which have a shortcut which is already used in the 'main part of' my application. This means that if press one of those shortcut key combinations, I get something like:
    QAction::eventFilter: Ambiguous shortcut overload: Ctrl+C
    Please note that the shortcut makes sense in both cases. It's just that depending on which widget is active (i.e. either my main window or a dockable window), I want to handle the action differently (e.g. if we think of the Ctrl+C shortcut, my main window might be displaying some text which I would like to copy while my dockable window might display some help using QWebView and I might want to copy the contents of that help).

    I have tried to 'play with' the shortcutContext property of QAction, but to no avail. So, question... is there a way to what I am after...?

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Same shortcut for different actions in different widgets

    I guess QSignalMapper would do the trick, but I never really got how it works.. nor I spent time trying to get it.
    Take a look at Qt MDI example, it's used here

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Same shortcut for different actions in different widgets

    See Qt::ShortcutContext for this and add the actions to the correct widgets!

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Same shortcut for different actions in different widgets

    My opinion is that you should re-think the keyboard shortcuts and note that for many of us (and likely many of your users) Ctrl+C means copy.

    And put your self in the "user" perspective, would you like to use an application which uses Ctrl+C all over the place and each time you press the key combination does a different thing depending on which widget has focus?

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Same shortcut for different actions in different widgets

    @Zlatomir: Think about a QDockWidget holding an Editor and about another QDockWidget holding an Console with output. Ctrl+C makes sense in both, but does something "different" depending on the Focus

  6. #6
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Same shortcut for different actions in different widgets

    Quote Originally Posted by totem View Post
    I guess QSignalMapper would do the trick, but I never really got how it works.. nor I spent time trying to get it.
    Take a look at Qt MDI example, it's used here
    Ok, having had a look at it, I don't think this is what I am after. It seems to me that QSignalMapper is used in the MDI example to get the MDI child windows activated. Also with regards to the 'shared' actions, I want to be able to have different tooltips, etc. and I can't see how QSignalMapper can address that.

    Quote Originally Posted by nightghost View Post
    See Qt::ShortcutContext for this and add the actions to the correct widgets!
    I tried that earlier, but maybe didn't do it right. Will try again...

    Quote Originally Posted by Zlatomir View Post
    My opinion is that you should re-think the keyboard shortcuts and note that for many of us (and likely many of your users) Ctrl+C means copy.

    And put your self in the "user" perspective, would you like to use an application which uses Ctrl+C all over the place and each time you press the key combination does a different thing depending on which widget has focus?
    You clearly haven't read my message. Try again.
    Last edited by agarny; 30th March 2011 at 17:27.

  7. #7
    Join Date
    May 2010
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    11

    Default Re: Same shortcut for different actions in different widgets

    In the application I've implemented there is a case where a QAction associated with a widget has the same shortcut as a different QAction associated with a menu item for the same user effect. The reason for this is irrelevant (has to do with needing distinct slots handling the QAction triggered signals in the different contexts). To avoid the ambiguous shortcut problem I employ setShortcutContext(Qt::WidgetWithChildrenShorcut) applied to the QAction used in the more restrictive context. This enables the "lower level" widget to which the restricted QAction has been applied to consume the shortcut key event and prevent it from "bubbling up" to the menu item with the other QAction that has the same shortcut, yet still allows shortcut key events generated "above" the lower level widget to reach the "higher level" menu item's QAction.

Similar Threads

  1. Can i add Actions to QDockWidget
    By ladiesfinger in forum Qt Programming
    Replies: 4
    Last Post: 30th January 2011, 15:11
  2. help with QSignalMapper and actions
    By andreime in forum Newbie
    Replies: 1
    Last Post: 9th August 2009, 18:24
  3. Replies: 2
    Last Post: 31st July 2009, 09:18
  4. Spacing widgets and actions in a toolbar
    By indifference in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2007, 22:59
  5. Actions and what to do with them?
    By krsmichael in forum Qt Tools
    Replies: 2
    Last Post: 20th January 2006, 22:08

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.