QGuiShortcut Class

The QGuiShortcut class is a base class for handling keyboard shortcuts. More...

Header: #include <QGuiShortcut>
qmake: QT += gui
Since: Qt 6.0
Inherits: QObject
Inherited By:


This class was introduced in Qt 6.0.


Public Functions

QGuiShortcut(const QKeySequence &key, QWindow *parent, const char *member = nullptr, const char *ambiguousMember = nullptr, Qt::ShortcutContext context = Qt::WindowShortcut)
QGuiShortcut(QWindow *parent)
virtual ~QGuiShortcut()
bool autoRepeat() const
Qt::ShortcutContext context() const
int id() const
bool isEnabled() const
QKeySequence key() const
void setAutoRepeat(bool on)
void setContext(Qt::ShortcutContext context)
void setEnabled(bool enable)
void setKey(const QKeySequence &key)


Detailed Description

The QGuiShortcut class is a base class for classes providing 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.

See also QShortcutEvent, QKeySequence, and QAction.

Property Documentation

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:

bool autoRepeat() const
void setAutoRepeat(bool on)

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:

Qt::ShortcutContext context() const
void setContext(Qt::ShortcutContext context)

enabled : bool

This property holds whether the shortcut is enabled

An enabled shortcut emits the activated() or activatedAmbiguously() signal when a QShortcutEvent occurs that matches the shortcut's key() 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.

Access functions:

bool isEnabled() const
void setEnabled(bool enable)

See also whatsThis.

key : QKeySequence

This property holds the shortcut's 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:

By default, this property contains an empty key sequence.

Access functions:

QKeySequence key() const
void setKey(const QKeySequence &key)

Member Function Documentation

QGuiShortcut::QGuiShortcut(const QKeySequence &key, QWindow *parent, const char *member = nullptr, const char *ambiguousMember = nullptr, Qt::ShortcutContext context = Qt::WindowShortcut)

Constructs a QGuiShortcut object for the parent window. The shortcut operates on its parent, listening for QShortcutEvents that match the key sequence. 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.

QGuiShortcut::QGuiShortcut(QWindow *parent)

Constructs a QGuiShortcut object for the parent window. Since no shortcut key sequence is specified, the shortcut will not emit any signals.

See also setKey().

[signal] void QGuiShortcut::activated()

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

See also activatedAmbiguously().

[signal] void QGuiShortcut::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 activated().

[virtual] QGuiShortcut::~QGuiShortcut()

Destroys the shortcut.

int QGuiShortcut::id() const

Returns the shortcut's ID.

See also QShortcutEvent::shortcutId().

© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.