Search code examples
c++qtqt4stylesheet

Is it possible to use an underlined letter as keyboard shortcut in Qt?


I want to create a button that can be triggered with a keyboard shortcut based on a letter of the button text; the letter should be underlined, in order to make the shortcut more visible.


Solution

  • Look at the QShortcut class:

    On certain widgets, using '&' in front of a character will automatically create a mnemonic (a shortcut) for that character, e.g. "E&xit" will create the shortcut Alt+X (use '&&' to display an actual ampersand). The widget might consume and perform an action on a given shortcut. On X11 the ampersand will not be shown and the character will be underlined. On Windows, shortcuts are normally not displayed until the user presses the Alt key, but this is a setting the user can change. On Mac, shortcuts are disabled by default. Call qt_set_sequence_auto_mnemonic() to enable them. However, because mnemonic shortcuts do not fit in with Aqua's guidelines, Qt will not show the shortcut character underlined.