QtBase  v6.3.1
Signals | Public Member Functions | Protected Member Functions | Properties | List of all members
QShortcut Class Reference

The QShortcut class is used to create keyboard shortcuts. More...

#include <qshortcut.h>

Inheritance diagram for QShortcut:
Inheritance graph
[legend]
Collaboration diagram for QShortcut:
Collaboration graph
[legend]

Signals

void activated ()
 
void activatedAmbiguously ()
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 

Public Member Functions

 QShortcut (QObject *parent)
 
 QShortcut (const QKeySequence &key, QObject *parent, const char *member=nullptr, const char *ambiguousMember=nullptr, Qt::ShortcutContext context=Qt::WindowShortcut)
 
 QShortcut (QKeySequence::StandardKey key, QObject *parent, const char *member=nullptr, const char *ambiguousMember=nullptr, Qt::ShortcutContext context=Qt::WindowShortcut)
 
template<typename Func1 >
 QShortcut (const QKeySequence &key, QObject *parent, Func1 slot1, Qt::ShortcutContext context=Qt::WindowShortcut)
 
template<class Obj1 , typename Func1 >
 QShortcut (const QKeySequence &key, QObject *parent, const Obj1 *object1, Func1 slot1, Qt::ShortcutContext context=Qt::WindowShortcut, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type *=0)
 
template<class Obj1 , typename Func1 , typename Func2 >
 QShortcut (const QKeySequence &key, QObject *parent, const Obj1 *object1, Func1 slot1, Func2 slot2, Qt::ShortcutContext context=Qt::WindowShortcut, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type *=0)
 
template<class Obj1 , typename Func1 , class Obj2 , typename Func2 >
 QShortcut (const QKeySequence &key, QObject *parent, const Obj1 *object1, Func1 slot1, const Obj2 *object2, Func2 slot2, Qt::ShortcutContext context=Qt::WindowShortcut, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type *=0, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj2 * >::Value >::type *=0)
 
template<typename Func1 >
 QShortcut (QKeySequence::StandardKey key, QObject *parent, Func1 slot1, Qt::ShortcutContext context=Qt::WindowShortcut)
 
template<class Obj1 , typename Func1 >
 QShortcut (QKeySequence::StandardKey key, QObject *parent, const Obj1 *object1, Func1 slot1, Qt::ShortcutContext context=Qt::WindowShortcut, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type *=0)
 
template<class Obj1 , typename Func1 , typename Func2 >
 QShortcut (QKeySequence::StandardKey key, QObject *parent, const Obj1 *object1, Func1 slot1, Func2 slot2, Qt::ShortcutContext context=Qt::WindowShortcut, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type *=0)
 
template<class Obj1 , typename Func1 , class Obj2 , typename Func2 >
 QShortcut (QKeySequence::StandardKey key, QObject *parent, const Obj1 *object1, Func1 slot1, const Obj2 *object2, Func2 slot2, Qt::ShortcutContext context=Qt::WindowShortcut, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type *=0, typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj2 * >::Value >::type *=0)
 
 ~QShortcut ()
 
void setKey (const QKeySequence &key)
 
QKeySequence key () const
 
void setKeys (QKeySequence::StandardKey key)
 
void setKeys (const QList< QKeySequence > &keys)
 
QList< QKeySequencekeys () const
 
void setEnabled (bool enable)
 
bool isEnabled () const
 
void setContext (Qt::ShortcutContext context)
 
Qt::ShortcutContext context () const
 
void setAutoRepeat (bool on)
 
bool autoRepeat () const
 
void setWhatsThis (const QString &text)
 
QString whatsThis () const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 
virtual ~QObject ()
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const
 
void setObjectName (const QString &name)
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 
bool isWindowType () const
 
bool signalsBlocked () const noexcept
 
bool blockSignals (bool b) noexcept
 
QThreadthread () const
 
void moveToThread (QThread *thread)
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 
template<typename T >
T findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
const QObjectListchildren () const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 
void dumpObjectInfo () const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 
bool inherits (const char *classname) const
 

Protected Member Functions

bool event (QEvent *e) override
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 

Properties

QKeySequence key
 the shortcut's primary key sequence More...
 
bool enabled
 whether the shortcut is enabled More...
 
bool autoRepeat
 whether the shortcut can auto repeat More...
 
Qt::ShortcutContext context
 the context in which the shortcut is valid More...
 
- Properties inherited from QObject
QString objectName
 the name of this object More...
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Detailed Description

The QShortcut class is used to create keyboard shortcuts.

\inmodule QtGui

The QShortcut class provides a way of connecting keyboard shortcuts to Qt's \l{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 \uicontrol Shift, \uicontrol Ctrl, and \uicontrol Alt.

\target mnemonic

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 \uicontrol 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 \uicontrol Alt key, but this is a setting the user can change. On Mac, shortcuts are disabled by default. Call \l 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:

When the user types the \l{QKeySequence}{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().

See also
QShortcutEvent, QKeySequence, QAction

Definition at line 54 of file qshortcut.h.

Constructor & Destructor Documentation

◆ QShortcut() [1/11]

QShortcut::QShortcut ( QObject parent)
explicit

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()

Definition at line 199 of file qshortcut.cpp.

Here is the call graph for this function:

◆ QShortcut() [2/11]

QShortcut::QShortcut ( const QKeySequence key,
QObject parent,
const char *  member = nullptr,
const char *  ambiguousMember = nullptr,
Qt::ShortcutContext  context = Qt::WindowShortcut 
)
explicit

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

The shortcut operates on its parent, listening for \l{QShortcutEvent}s 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.

Definition at line 214 of file qshortcut.cpp.

Here is the call graph for this function:

◆ QShortcut() [3/11]

QShortcut::QShortcut ( QKeySequence::StandardKey  standardKey,
QObject parent,
const char *  member = nullptr,
const char *  ambiguousMember = nullptr,
Qt::ShortcutContext  context = Qt::WindowShortcut 
)
explicit
Since
6.0 Constructs a QShortcut object for the parent, which should be a QWindow or a QWidget.

The shortcut operates on its parent, listening for \l{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.

Definition at line 241 of file qshortcut.cpp.

Here is the call graph for this function:

◆ QShortcut() [4/11]

template<typename Func1 >
QShortcut::QShortcut ( const QKeySequence key,
QObject parent,
Func1  slot1,
Qt::ShortcutContext  context = Qt::WindowShortcut 
)
inline

Definition at line 111 of file qshortcut.h.

Here is the call graph for this function:

◆ QShortcut() [5/11]

template<class Obj1 , typename Func1 >
QShortcut::QShortcut ( const QKeySequence key,
QObject parent,
const Obj1 *  object1,
Func1  slot1,
Qt::ShortcutContext  context = Qt::WindowShortcut,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type = 0 
)
inline

Definition at line 119 of file qshortcut.h.

Here is the call graph for this function:

◆ QShortcut() [6/11]

template<class Obj1 , typename Func1 , typename Func2 >
QShortcut::QShortcut ( const QKeySequence key,
QObject parent,
const Obj1 *  object1,
Func1  slot1,
Func2  slot2,
Qt::ShortcutContext  context = Qt::WindowShortcut,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type = 0 
)
inline

Definition at line 128 of file qshortcut.h.

Here is the call graph for this function:

◆ QShortcut() [7/11]

template<class Obj1 , typename Func1 , class Obj2 , typename Func2 >
QShortcut::QShortcut ( const QKeySequence key,
QObject parent,
const Obj1 *  object1,
Func1  slot1,
const Obj2 *  object2,
Func2  slot2,
Qt::ShortcutContext  context = Qt::WindowShortcut,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type = 0,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj2 * >::Value >::type = 0 
)
inline

Definition at line 138 of file qshortcut.h.

Here is the call graph for this function:

◆ QShortcut() [8/11]

template<typename Func1 >
QShortcut::QShortcut ( QKeySequence::StandardKey  key,
QObject parent,
Func1  slot1,
Qt::ShortcutContext  context = Qt::WindowShortcut 
)
inline

Definition at line 151 of file qshortcut.h.

Here is the call graph for this function:

◆ QShortcut() [9/11]

template<class Obj1 , typename Func1 >
QShortcut::QShortcut ( QKeySequence::StandardKey  key,
QObject parent,
const Obj1 *  object1,
Func1  slot1,
Qt::ShortcutContext  context = Qt::WindowShortcut,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type = 0 
)
inline

Definition at line 159 of file qshortcut.h.

Here is the call graph for this function:

◆ QShortcut() [10/11]

template<class Obj1 , typename Func1 , typename Func2 >
QShortcut::QShortcut ( QKeySequence::StandardKey  key,
QObject parent,
const Obj1 *  object1,
Func1  slot1,
Func2  slot2,
Qt::ShortcutContext  context = Qt::WindowShortcut,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type = 0 
)
inline

Definition at line 168 of file qshortcut.h.

Here is the call graph for this function:

◆ QShortcut() [11/11]

template<class Obj1 , typename Func1 , class Obj2 , typename Func2 >
QShortcut::QShortcut ( QKeySequence::StandardKey  key,
QObject parent,
const Obj1 *  object1,
Func1  slot1,
const Obj2 *  object2,
Func2  slot2,
Qt::ShortcutContext  context = Qt::WindowShortcut,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj1 * >::Value >::type = 0,
typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< Obj2 * >::Value >::type = 0 
)
inline

Definition at line 178 of file qshortcut.h.

Here is the call graph for this function:

◆ ~QShortcut()

QShortcut::~QShortcut ( )

Destroys the shortcut.

Definition at line 372 of file qshortcut.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ activated

void QShortcut::activated ( )
signal

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

See also
activatedAmbiguously()
Here is the caller graph for this function:

◆ activatedAmbiguously

void QShortcut::activatedAmbiguously ( )
signal

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()
Here is the caller graph for this function:

◆ autoRepeat()

bool QShortcut::autoRepeat ( ) const

Definition at line 531 of file qshortcut.cpp.

◆ context()

Qt::ShortcutContext QShortcut::context ( ) const

Definition at line 505 of file qshortcut.cpp.

◆ event()

bool QShortcut::event ( QEvent e)
overrideprotectedvirtual

This virtual function receives events to an object and should return true if the event e was recognized and processed.

The event() function can be reimplemented to customize the behavior of an object.

Make sure you call the parent event class implementation for all the events you did not handle.

Example:

See also
installEventFilter(), timerEvent(), QCoreApplication::sendEvent(), QCoreApplication::postEvent()

Reimplemented from QObject.

Definition at line 595 of file qshortcut.cpp.

Here is the call graph for this function:

◆ isEnabled()

bool QShortcut::isEnabled ( ) const

Definition at line 477 of file qshortcut.cpp.

◆ key()

QKeySequence QShortcut::key ( ) const

Definition at line 400 of file qshortcut.cpp.

◆ keys()

QList< QKeySequence > QShortcut::keys ( ) const

Returns the list of key sequences which trigger this shortcut.

Since
6.0
See also
key, setKeys()

Definition at line 445 of file qshortcut.cpp.

Here is the caller graph for this function:

◆ setAutoRepeat()

void QShortcut::setAutoRepeat ( bool  on)

Definition at line 520 of file qshortcut.cpp.

Here is the call graph for this function:

◆ setContext()

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

Definition at line 495 of file qshortcut.cpp.

Here is the call graph for this function:

◆ setEnabled()

void QShortcut::setEnabled ( bool  enable)

Definition at line 466 of file qshortcut.cpp.

Here is the call graph for this function:

◆ setKey()

void QShortcut::setKey ( const QKeySequence key)

Definition at line 392 of file qshortcut.cpp.

Here is the call graph for this function:

◆ setKeys() [1/2]

void QShortcut::setKeys ( const QList< QKeySequence > &  keys)

Sets keys as the list of key sequences that trigger the shortcut.

Since
6.0
See also
key, keys()

Definition at line 416 of file qshortcut.cpp.

Here is the call graph for this function:

◆ setKeys() [2/2]

void QShortcut::setKeys ( QKeySequence::StandardKey  key)

Sets the triggers to those matching the standard key key.

Since
6.0
See also
key, keys()

Definition at line 433 of file qshortcut.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setWhatsThis()

void QShortcut::setWhatsThis ( const QString text)

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
QWhatsThis::inWhatsThisMode(), QAction::setWhatsThis()

Definition at line 554 of file qshortcut.cpp.

◆ whatsThis()

QString QShortcut::whatsThis ( ) const

Returns the shortcut's "What's This?" help text.

See also
setWhatsThis()

Definition at line 565 of file qshortcut.cpp.

Property Documentation

◆ autoRepeat

QShortcut::autoRepeat
readwrite

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.

Definition at line 233 of file qshortcut.h.

◆ context

QShortcut::context
readwrite

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.

Definition at line 233 of file qshortcut.h.

◆ enabled

QShortcut::enabled
readwrite

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.

See also
whatsThis

Definition at line 233 of file qshortcut.h.

◆ key

QShortcut::key
readwrite

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:

By default, this property contains an empty key sequence.

Definition at line 233 of file qshortcut.h.


The documentation for this class was generated from the following files: