[Qt-interest] How install shortcuts for multiple widgets?

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Wed Feb 9 13:28:10 CET 2011


On 2011-02-09 Espen Espen Sture Arnesen wrote:

> > ...
> In short, the QAction is one instance that does one thing and I access the same
> QAction from several widgets.

Usually that is exactly the idea behind a QAction: you create it /once/, and you can add it to /multiple/ widgets, typically a menu and a toolbar.

But this assumes that when the action is triggered it should always do the /same/ thing ("...the QAction is one instance that does on thing...").

But from what I understand the OP has a different use case: there are several editors, and depending which editor has the focus pressing CTRL + S should trigger the "Save action" specific to that editor, or in other words: what needs to be saved is depending on the editor and is /different/ (not the same document to be saved) each time!

A workaround could be to still have one single "Save action" instance, connect it to a single slot and inside that slot define what to really save, depending on what editor had the focus last.

In my use case however I create a new main window for each new document (that seems to be the thing to do nowadays, especially on Mac. I am not familiar with "setting a shortcut-context" (but it sounds like this would be the way to resolve these ambiguities). Having a main window (QMainWindow) for each document and a file menu with each a "Save CTRL + S" QAction in there works as expected: the slot of the main window instance which has focus is called. So at least on a "per main window" basis the distinction between shortcuts seems to work without further ado.

My educated guess would be that this "shortcut context" thing might only work between "dialog and main windows", but not between common QWidgets?


Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22




More information about the Qt-interest-old mailing list