- class QShortcut¶
The
QShortcut
class is used to create keyboard shortcuts. More…Synopsis¶
Properties¶
autoRepeatᅟ
- Whether the shortcut can auto repeatcontextᅟ
- The context in which the shortcut is validenabledᅟ
- Whether the shortcut is enabledkeyᅟ
- The shortcut’s primary key sequence
Methods¶
def
__init__()
def
autoRepeat()
def
context()
def
id()
def
isEnabled()
def
key()
def
keys()
def
setAutoRepeat()
def
setContext()
def
setEnabled()
def
setKey()
def
setKeys()
def
setWhatsThis()
def
whatsThis()
Signals¶
def
activated()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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’sactivated()
signal is emitted. (In the case of ambiguity, theactivatedAmbiguously()
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 withkey()
. A shortcut can be enabled or disabled withsetEnabled()
, and can have “What’s This?” help text set withsetWhatsThis()
.See also
Note
Properties can be used directly when
from __feature__ import true_property
is used or via accessor functions otherwise.- property autoRepeatᅟ: bool¶
This property holds whether the shortcut can auto repeat.
If true, the shortcut will auto repeat when the keyboard shortcut combination is held down, provided that keyboard auto repeat is enabled on the system. The default value is true.
- Access functions:
- property contextᅟ: Qt.ShortcutContext¶
This property holds the context in which the shortcut is valid.
A shortcut’s context decides in which circumstances a shortcut is allowed to be triggered. The normal context is Qt::WindowShortcut, which allows the shortcut to trigger if the parent (the widget containing the shortcut) is a subwidget of the active top-level window.
By default, this property is set to Qt::WindowShortcut.
- Access functions:
- property enabledᅟ: bool¶
This property holds whether the shortcut is enabled.
An enabled shortcut emits the
activated()
oractivatedAmbiguously()
signal when aQShortcutEvent
occurs that matches the shortcut’skey()
sequence.If the application is in
WhatsThis
mode the shortcut will not emit the signals, but will show the “What’s This?” text instead.By default, this property is
true
.See also
- Access functions:
- property keyᅟ: QKeySequence¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
This property holds the shortcut’s primary key sequence.
This is a key sequence with an optional combination of Shift, Ctrl, and Alt. The key sequence may be supplied in a number of ways:
setKey(0) # no signal emitted setKey(QKeySequence()) # no signal emitted setKey(0x3b1) # Greek letter alpha setKey(Qt.Key_D) # 'd', e.g. to delete setKey('q') # 'q', e.g. to quit setKey(Qt.CTRL | Qt.Key_P) # Ctrl+P, e.g. to print document setKey("Ctrl+P") # Ctrl+P, e.g. to print document
By default, this property contains an empty key sequence.
Constructs a
QShortcut
object for theparent
, which should be aQWindow
or a QWidget.Since no shortcut key sequence is specified, the shortcut will not emit any signals.
See also
- __init__(arg__1, arg__2, arg__3[, arg__4=Qt.WindowShortcut])
- Parameters:
arg__1 –
QKeySequence
arg__2 –
QObject
arg__3 –
PyCallable
arg__4 –
ShortcutContext
- __init__(arg__1, arg__2, arg__3[, arg__4=Qt.WindowShortcut])
- Parameters:
arg__1 –
StandardKey
arg__2 –
QObject
arg__3 –
PyCallable
arg__4 –
ShortcutContext
- __init__(key, parent[, member=None[, ambiguousMember=None[, context=Qt.WindowShortcut]]])
- Parameters:
key –
StandardKey
parent –
QObject
member – str
ambiguousMember – str
context –
ShortcutContext
Constructs a
QShortcut
object for theparent
, which should be aQWindow
or a QWidget.The shortcut operates on its parent, listening for
QShortcutEvent
s that match thestandardKey
. Depending on the ambiguity of the event, the shortcut will call themember
function, or theambiguousMember
function, if the key press was in the shortcut’scontext
.- __init__(key, parent[, member=None[, ambiguousMember=None[, context=Qt.WindowShortcut]]])
- Parameters:
key –
QKeySequence
parent –
QObject
member – str
ambiguousMember – str
context –
ShortcutContext
Constructs a
QShortcut
object for theparent
, which should be aQWindow
or a QWidget.The shortcut operates on its parent, listening for
QShortcutEvent
s that match thekey
sequence. Depending on the ambiguity of the event, the shortcut will call themember
function, or theambiguousMember
function, if the key press was in the shortcut’scontext
.- activated()¶
This signal is emitted when the user types the shortcut’s key sequence.
See also
- 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, activatedAmbiguously() 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
- autoRepeat()¶
- Return type:
bool
See also
Getter of property
autoRepeatᅟ
.- context()¶
- Return type:
See also
Getter of property
contextᅟ
.- id()¶
- Return type:
int
Note
This function is deprecated.
Returns the primary key binding’s ID.
See also
- isEnabled()¶
- Return type:
bool
Getter of property
enabledᅟ
.- key()¶
- Return type:
See also
Getter of property
keyᅟ
.- keys()¶
- Return type:
.list of QKeySequence
Returns the list of key sequences which trigger this shortcut.
- setAutoRepeat(on)¶
- Parameters:
on – bool
See also
Setter of property
autoRepeatᅟ
.- setContext(context)¶
- Parameters:
context –
ShortcutContext
See also
Setter of property
contextᅟ
.- setEnabled(enable)¶
- Parameters:
enable – bool
See also
Setter of property
enabledᅟ
.- setKey(key)¶
- Parameters:
key –
QKeySequence
See also
Setter of property
keyᅟ
.- setKeys(key)¶
- Parameters:
key –
StandardKey
Sets the triggers to those matching the standard key
key
.- setKeys(keys)
- Parameters:
keys – .list of QKeySequence
Sets
keys
as the list of key sequences that trigger the shortcut.- 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()¶
- Return type:
str
Returns the shortcut’s “What’s This?” help text.
See also