PDA

View Full Version : HowTo: Handle shortcuts



Wurgl
22nd December 2014, 00:51
I have a question regarding shortcuts, lets take the Windows-style rename "F2" as an example.

In my application I have some places where I can do such a rename, for example some drawing area, depending on some selection that rename is active or inactive. Currently it is accessible by a menu in the menubar of QMainWindow. So when whatever thing in that drawing area is selected, the corresponding QAction in a menu is enabled, otherwise disabled.

Works fine.

Then there is some other area in the same QMainWindow, in my application a tree like structure. When I select an item in that tree, pressing F2 shall starting editing the selected QStandardItem.

Works fine too -- but not always, sometimes i do not get a call of keyPressEvent() in the QTreeView.

Well, my problem here is that the key press gets eaten by the QAction in the menu when that QAction is active. Now I have two possible solutions:
* enable/disable that menu entry a "thousand" times, whenever there is nothing selected in that drawing area, whenever the mouse leaves that drawing area and maybe depending on some more activities.

* in the slot handling the signal from the QAction in the menu, determine what subwidget or what part of the main window is active and do there the rename in the drawing area or the rename in the tree structure. The logic with keyPressEvent() inside the QTreeView will still exist in case the QAction in the menu is disabled.

The first solution has the disadvantage that I may miss some enabling or disabling, so the rename does not start. I am a little bit afraid, that there may be such problems again and again. Don't like that.

The second one means (maybe) a lot of duplicated code and stronger coupled code, and for the same user activity I run sometimes thru this code and sometime thru that code, so this is not very nice too.

How would you implement such shortcuts?