PDA

View Full Version : Same shortcut for different actions in different widgets



agarny
30th March 2011, 15:34
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+CPlease 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...?

totem
30th March 2011, 16:33
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

nightghost
30th March 2011, 16:44
See Qt::ShortcutContext (http://doc.qt.nokia.com/4.7/qshortcut.html#context-prop) for this and add the actions to the correct widgets!

Zlatomir
30th March 2011, 16:44
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?

nightghost
30th March 2011, 16:47
@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 :)

agarny
30th March 2011, 16:51
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 hereOk, 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.


See Qt::ShortcutContext (http://doc.qt.nokia.com/4.7/qshortcut.html#context-prop) for this and add the actions to the correct widgets!I tried that earlier, but maybe didn't do it right. Will try again...


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.

bcastalia
13th May 2011, 08:38
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.