QShortcut

The QShortcut class is used to create keyboard shortcuts. More

Inheritance diagram of PySide6.QtGui.QShortcut

Synopsis

Functions

Detailed Description

The QShortcut class provides a way of connecting keyboard shortcuts to Qt’s signals and slots mechanism, so that objects can be informed when a shortcut is executed. The shortcut can be set up to contain all the key presses necessary to describe a keyboard shortcut, including the states of modifier keys such as Shift, Ctrl, and Alt.

In widget applications, certain widgets can use ‘&’ in front of a character. This 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.

For applications that use menus, it may be more convenient to use the convenience functions provided in the QMenu class to assign keyboard shortcuts to menu items as they are created. Alternatively, shortcuts may be associated with other types of actions in the QAction class.

The simplest way to create a shortcut for a particular widget is to construct the shortcut with a key sequence. For example:

shortcut = QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")),
                         parent)

When the user types the key sequence for a given shortcut, the shortcut’s activated() signal is emitted. (In the case of ambiguity, the activatedAmbiguously() signal is emitted.) A shortcut is “listened for” by Qt’s event loop when the shortcut’s parent widget is receiving events.

A shortcut’s key sequence can be set with setKey() and retrieved with key() . A shortcut can be enabled or disabled with setEnabled() , and can have “What’s This?” help text set with setWhatsThis() .

class PySide6.QtGui.QShortcut(arg__1, arg__2, arg__3[, arg__4=Qt.WindowShortcut])

PySide6.QtGui.QShortcut(arg__1, arg__2, arg__3[, arg__4=Qt.WindowShortcut])

PySide6.QtGui.QShortcut(key, parent[, member=None[, ambiguousMember=None[, context=Qt.WindowShortcut]]])

PySide6.QtGui.QShortcut(parent)

PySide6.QtGui.QShortcut(key, parent[, member=None[, ambiguousMember=None[, context=Qt.WindowShortcut]]])

Parameters

Constructs a QShortcut object for the parent, which should be a QWindow or a QWidget .

The shortcut operates on its parent, listening for QShortcutEvent s that match the standardKey. Depending on the ambiguity of the event, the shortcut will call the member function, or the ambiguousMember function, if the key press was in the shortcut’s context.

Constructs a QShortcut object for the parent, which should be a QWindow or a QWidget .

Since no shortcut key sequence is specified, the shortcut will not emit any signals.

See also

setKey()

PySide6.QtGui.QShortcut.activated()

This signal is emitted when the user types the shortcut’s key sequence.

PySide6.QtGui.QShortcut.activatedAmbiguously()

When a key sequence is being typed at the keyboard, it is said to be ambiguous as long as it matches the start of more than one shortcut.

When a shortcut’s key sequence is completed, is emitted if the key sequence is still ambiguous (i.e., it is the start of one or more other shortcuts). The activated() signal is not emitted in this case.

See also

activated()

PySide6.QtGui.QShortcut.autoRepeat()
Return type

bool

See also

setAutoRepeat()

PySide6.QtGui.QShortcut.context()
Return type

ShortcutContext

See also

setContext()

PySide6.QtGui.QShortcut.id()
Return type

int

Note

This function is deprecated.

Returns the primary key binding’s ID.

See also

shortcutId()

PySide6.QtGui.QShortcut.isEnabled()
Return type

bool

PySide6.QtGui.QShortcut.key()
Return type

PySide6.QtGui.QKeySequence

See also

setKey()

PySide6.QtGui.QShortcut.keys()
Return type

Returns the list of key sequences which trigger this shortcut.

See also

key setKeys()

PySide6.QtGui.QShortcut.setAutoRepeat(on)
Parameters

on – bool

See also

autoRepeat()

PySide6.QtGui.QShortcut.setContext(context)
Parameters

contextShortcutContext

See also

context()

PySide6.QtGui.QShortcut.setEnabled(enable)
Parameters

enable – bool

See also

isEnabled()

PySide6.QtGui.QShortcut.setKey(key)
Parameters

keyPySide6.QtGui.QKeySequence

See also

key()

PySide6.QtGui.QShortcut.setKeys(key)
Parameters

keyStandardKey

Sets the triggers to those matching the standard key key.

See also

key keys()

PySide6.QtGui.QShortcut.setKeys(keys)
Parameters

keys

PySide6.QtGui.QShortcut.setWhatsThis(text)
Parameters

text – str

Sets the shortcut’s “What’s This?” help text.

The text will be shown when a widget application is in “What’s This?” mode and the user types the shortcut key() sequence.

To set “What’s This?” help on a menu item (with or without a shortcut key), set the help on the item’s action.

By default, the help text is an empty string.

This function has no effect in applications that don’t use widgets.

See also

whatsThis() inWhatsThisMode() setWhatsThis()

PySide6.QtGui.QShortcut.whatsThis()
Return type

str

Returns the shortcut’s “What’s This?” help text.

See also

setWhatsThis()