views:

38

answers:

2

Hi all,

I've got a sizable Qt app that has been in development since the Qt 3 days, and it now contains dozens of windows with thousands of menu items, controls, and other user-initiatable actions. It currently compiles under Qt 4.6, for Linux, MacOS/X, and Windows.

The new feature request from on high is that the user should be able to customize any and all keyboard shortcuts in this app... i.e. there should be a "Customize Key Bindings..." menu item, that when chosen, opens up a dialog that lists all of the actions in the application and their current key binding (if any) and allows the user to assign or change key bindings for any and all actions he cares to, and then save his settings and use the applications with his own customized key bindings.

This seems like a rather ambitious thing to implement, considering the number of keyboard-able actions in the app, and I'm wondering if there is any existing classes or code libraries available to assist in this sort of thing, or if it's something I'm going to have to implement from scratch myself. The Qt internationalization system, in particular, seems like it might be adapted to help with something like this -- the difference being that instead of (actually in addition to) the developer choosing key combinations before shipping the app, the users could choose/alter key combinations while using the app (if they aren't happy with the shipped defaults, of course).

Does anyone have any hints or pointers on code or approaches towards implementing this feature?

A: 

You could store the bindings in an application config file and read it in upon app startup. Then whenever the user changes bindings update this file. Keys are just enums in the Qt framework. You can override the appropriate keyPressEvent() or keyReleaseEvent(), check the key(s) pressed and match against the current bindings.

JimDaniel
True -- but this is what I would call "writing it from scratch". Since this seems like a non-application-specific feature, I was wondering if there is some already available QKeyBindingsDialog class or something somewhere that I could drop in.
Jeremy Friesner
I suppose you could make it generic using QActions with a little effort, but I'm unaware of any existing widget for this purpose. If you do make it generic, you should post it somewhere others can use it :-)
JimDaniel
+1  A: 

I agree with JimDaniel, it sounds like the most generic approach would be to create a QAction for everything that you would to be executed through a Keyboard shortcut. The user then configures the appropriate shortcut for each action.

This is definitely a cleaner way to implement this than overriding the events, it also then lets you put your actions into menus and toolbars, I don't know how much work this would be for your application.

Harald Scheirich