CopperSpice API  1.9.2
QShortcut Class Reference

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

Inheritance diagram for QShortcut:
QObject

Public Signals

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

Public Methods

 QShortcut (const QKeySequence &key, QWidget *parent, const QString &member=QString (), const QString &ambiguousMember=QString (), Qt::ShortcutContext context=Qt::WindowShortcut)
 
 QShortcut (QWidget *parent)
 
 ~QShortcut ()
 
bool autoRepeat () const
 
Qt::ShortcutContext context () const
 
int id () const
 
bool isEnabled () const
 
QKeySequence key () const
 
QWidgetparentWidget () const
 
void setAutoRepeat (bool on)
 
void setContext (Qt::ShortcutContext context)
 
void setEnabled (bool enable)
 
void setKey (const QKeySequence &key)
 
void setWhatsThis (const QString &text)
 
QString whatsThis () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

bool event (QEvent *event) override
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Properties

 autoRepeat
 
 context
 
 enabled
 
 key
 
 whatsThis
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Detailed Description

The QShortcut class is used to create keyboard shortcuts. This class is responsible for emitting a signal when a keyboard shortcut is pressed. For information about connecting a signal to a slot method refer to the to the section about signals and slots.

A shortcut can be configured to contain all the key presses necessary to describe a keyboard shortcut, including the states of modifier keys such as Shift, Ctrl, and Alt.

On certain widgets using "&amp;" in front of a character will automatically create a shortcut for the specific character after the ampersand. For example, "&Exit" will create the shortcut Alt+X. Use "&amp;&amp;" to display an actual ampersand.

Creating a Shortcut

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

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

When the user types the key sequence for a given shortcut the activated() signal is emitted. If the key sequence is ambiguous the activatedAmbiguously() signal is emitted instead.

A shortcut 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().

Menu Shortcuts

For applications which use menus it may be more convenient to use the methods provided in the QMenu class to assign keyboard shortcuts to menu items. Alternatively, shortcuts may be associated with other types of actions in the QAction class.

Platform Specific Behavior

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 OS X shortcuts are disabled by default. Call qt_set_sequence_auto_mnemonic() to enable them. Since keyboard shortcuts do not fit with Aqua style guidelines, CopperSpice will not show the shortcut character underlined.

See also
QShortcutEvent, QKeySequence, QAction

Constructor & Destructor Documentation

QShortcut::QShortcut ( QWidget parent)
explicit

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

See also
setKey()
QShortcut::QShortcut ( const QKeySequence key,
QWidget parent,
const QString member = QString(),
const QString ambiguousMember = QString(),
Qt::ShortcutContext  context = Qt::WindowShortcut 
)

Constructs a QShortcut object for the parent widget. 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.

QShortcut::~QShortcut ( )

Destroys the shortcut.

Method Documentation

void QShortcut::activated ( )
signal

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

See also
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()
bool QShortcut::autoRepeat ( ) const

Returns the value of the property.

Qt::ShortcutContext QShortcut::context ( ) const

Returns the value of the property.

bool QShortcut::event ( QEvent event)
overrideprotectedvirtual

Receives events to an object and should return true if the given event was recognized and processed. This method can be overridden to customize the event handling for an object.

See also
installEventFilter(), timerEvent(), QApplication::sendEvent(), QApplication::postEvent(), QWidget::event()

Reimplemented from QObject::event()

int QShortcut::id ( ) const

Returns the shortcut's ID.

See also
QShortcutEvent::shortcutId()
bool QShortcut::isEnabled ( ) const

Returns the value of the property.

QKeySequence QShortcut::key ( ) const

Returns the value of the property.

QWidget * QShortcut::parentWidget ( ) const
inline

Returns the shortcut's parent widget.

void QShortcut::setAutoRepeat ( bool  on)

Sets the value of the property to on.

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

Sets the value of the property to context.

void QShortcut::setEnabled ( bool  enable)

Sets the value of the property to enable.

void QShortcut::setKey ( const QKeySequence key)

Sets the value of the property to key.

void QShortcut::setWhatsThis ( const QString text)

Sets the value of the property to text.

QString QShortcut::whatsThis ( ) const

Returns the value of the property.

Property Documentation

QShortcut::autoRepeat

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.

PropertiesClass Methods
read autoRepeat
write setAutoRepeat
QShortcut::context

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.

PropertiesClass Methods
read context
write setContext
QShortcut::enabled

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.

See also
whatsThis
PropertiesClass Methods
read isEnabled
write setEnabled
QShortcut::key

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 as shown below.

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.

PropertiesClass Methods
read key
write setKey
QShortcut::whatsThis

This property holds the shortcut's "What's This?" help text. The text will be shown when the 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 set the help on the item's action. By default, this property contains an empty string.

See also
QWhatsThis::inWhatsThisMode(), QAction::setWhatsThis()
PropertiesClass Methods
read whatsThis
write setWhatsThis