Can you configure key event handling in Qt Designer (PyQt5)?

Tags: , , , ,



I just discovered Qt Designer today and it’s rather wonderful.

But I’m a keyboard fanatic and don’t want to be forced to use the mouse any more than is strictly necessary, or at the very least I want to have hotkey alternatives to any buttons, etc.

This can be coded manually but I was just wondering whether you can somehow set this up through Qt Designer? It would be good if this kind of configuration could be kept in the auto-generated .ui file (and then be converted to a .py file), rather than cluttering up the business code.

There is an item under “Input Widgets” called “Key Sequence Edit”… but I haven’t worked out what this does. It doesn’t seem to be the answer. I’ve also tried right-clicking on a widget (e.g. a QTreeView) but I couldn’t find an obvious answer there.

There’s also something called “Action Editor” (second tab in the bottom right box)… looks promising but I haven’t a clue what to do currently.

Answer

There is an important distinction that is required for this question. Note that this is valid for almost any GUI based environment/framework, not just Qt.

Mnemonics

I will cite the description in the related Wikipedia article:

A mnemonic is an underlined alphanumeric character, typically appearing in a menu title, menu item, or the text of a button or component of the user interface. A mnemonic indicates to the user which key to press (in conjunction with the Alt key) to activate a command or navigate to a component.

Depending on the platform and framework in use, but most importantly the source of the mnemonic, the behavior can slightly change.

For buttons or “buddies” (labels that are linked to another widget in order to use their mnemonic to set the focus on that widget), the mnemonic usually works with Alt+letter.
For menu items, instead, it’s usually enough to directly press the letter without the Alt modifier.

Keyboard shortcuts

They are (usually) keyboard combinations, using at least one or more modifiers and another standard (possibly) alphanumeric key.
A typical example is Ctrl+A, used to select everything in the current object.

Note that while conceptually a mnemonic _is_ a keyboard shortcut, and technically you can construct a keyboard shortcut without any modifier, it’s common convention to differentiate them (and avoid shortcuts without modifers).

Qt paradygm and use in Designer

In Qt mnemonics exist in the following cases:

  • actions (when they’re added to a QMenu or a QToolbar)
  • buttons (QAbstractButton subclasses)
  • tabs (QTabBar tabs, usually for QTabWidget)
  • group boxes (QGroupBox)
  • labels (using setBuddy()

In all situations they are shown with the mnemonic letter underlined[1], and that letter can be explicitly set using the & character preceding the letter that is going to be used (the ampersand can be actually shown using &&), otherwise the style will autonomously try to set them.

An important note about the last case (letting Qt set the mnemonics). The style set for the application (the default one or that set using setStyle()) or for a specific widget not only sets mnemonics on its own[2], but it will also overwrite the object text() property. If you create a button with its text as “Button”, not only Qt will probably use the “b” letter for its mnemonic, but button.text() will also return “&Button”. Note that this does not happen within the creation, but only as soon as the widget is added to a parent and that parent is shown.
That is another very important reason for which object comparison (if someWidget.text() == 'someText': do something) should never be done using the text() property.

An other important aspect to consider about mnemonics is that only the first &-preceded letter will be used for the mnemonic. If you create a button with text “&Some &button”, only the “S” letter will be considered (even if the “b” will still be probably underlined). Not only: if you try to use the same multiple &-based mnemonics on different widgets, the result will be undefined[3].

Now. Qt Designer doesn’t allow to create shortcuts for specific widgets arbitrarily, and that’s for good reasons: keyboard shortcuts should be unique and need context. UIs created with Designer should be as much universal as possible and should not go too “deep” in implementations that should be done on the logic side of the programming. QActions are designed for that, to be “actions” possibly executed out of any context, unlike standard widgets which depend on keyboard focus and mouse position, and that’s why you can set shortcuts for them.

Finally.
If you are just looking for mnemonics, follow the above suggestions about them.
If you need something that could actually be activated from anywhere, create a QAction, and connect its triggered signal to the related function within the code; if that something is going to appear on menus or toolbars also, you can create the related QAction on Designer.

Notes:
1 Some styles (notably, “breeze”) only shows underlined letters when the Alt is pressed.
2 Automatic mnemonics are created in insertion order to their parent, recursively.
3 Try creating two buttons with the text “&test &button”: the second button will probably appear as “test &button”.



Source: stackoverflow