![]() |
Qt 6.x
The Qt SDK
|
The QKeySequence class encapsulates a key sequence as used by shortcuts. More...
#include <qkeysequence.h>
Public Member Functions | |
QKeySequence () | |
Constructs an empty key sequence. | |
QKeySequence (const QString &key, SequenceFormat format=NativeText) | |
Creates a key sequence from the key string, based on format. | |
QKeySequence (int k1, int k2=0, int k3=0, int k4=0) | |
Constructs a key sequence with up to 4 keys k1, k2, k3 and k4. | |
QKeySequence (QKeyCombination k1, QKeyCombination k2=QKeyCombination::fromCombined(0), QKeyCombination k3=QKeyCombination::fromCombined(0), QKeyCombination k4=QKeyCombination::fromCombined(0)) | |
Constructs a key sequence with up to 4 keys k1, k2, k3 and k4. | |
QKeySequence (const QKeySequence &ks) | |
Copy constructor. | |
QKeySequence (StandardKey key) | |
~QKeySequence () | |
Destroys the key sequence. | |
int | count () const |
Returns the number of keys in the key sequence. | |
bool | isEmpty () const |
Returns true if the key sequence is empty; otherwise returns false. | |
QString | toString (SequenceFormat format=PortableText) const |
SequenceMatch | matches (const QKeySequence &seq) const |
Matches the sequence with seq. | |
operator QVariant () const | |
Returns the key sequence as a QVariant. | |
QKeyCombination | operator[] (uint i) const |
Returns a reference to the element at position index in the key sequence. | |
QKeySequence & | operator= (const QKeySequence &other) |
Move-assigns other to this QKeySequence instance. | |
void | swap (QKeySequence &other) noexcept |
bool | operator== (const QKeySequence &other) const |
Returns true if this key sequence is equal to the other key sequence; otherwise returns false . | |
bool | operator!= (const QKeySequence &other) const |
Returns true if this key sequence is not equal to the other key sequence; otherwise returns false . | |
bool | operator< (const QKeySequence &ks) const |
Provides an arbitrary comparison of this key sequence and other key sequence. | |
bool | operator> (const QKeySequence &other) const |
Returns true if this key sequence is larger than the other key sequence; otherwise returns false . | |
bool | operator<= (const QKeySequence &other) const |
Returns true if this key sequence is smaller or equal to the other key sequence; otherwise returns false . | |
bool | operator>= (const QKeySequence &other) const |
Returns true if this key sequence is larger or equal to the other key sequence; otherwise returns false . | |
bool | isDetached () const |
DataPtr & | data_ptr () |
Static Public Member Functions | |
static QKeySequence | fromString (const QString &str, SequenceFormat format=PortableText) |
static QList< QKeySequence > | listFromString (const QString &str, SequenceFormat format=PortableText) |
static QString | listToString (const QList< QKeySequence > &list, SequenceFormat format=PortableText) |
static QKeySequence | mnemonic (const QString &text) |
Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics are found. | |
static QList< QKeySequence > | keyBindings (StandardKey key) |
Friends | |
class | QShortcutMap |
class | QShortcut |
Q_GUI_EXPORT QDataStream & | operator<< (QDataStream &in, const QKeySequence &ks) |
Writes the key sequence to the stream. | |
Q_GUI_EXPORT QDataStream & | operator>> (QDataStream &in, QKeySequence &ks) |
Reads a key sequence from the stream into the key sequence. | |
Q_GUI_EXPORT size_t | qHash (const QKeySequence &key, size_t seed) noexcept |
Related Symbols | |
(Note that these are not member symbols.) | |
void | qt_set_sequence_auto_mnemonic (bool b) |
Specifies whether mnemonics for menu items, labels, etc., should be honored or not. | |
size_t | qHash (const QKeySequence &key, size_t seed) noexcept |
QDataStream & | operator<< (QDataStream &stream, const QKeySequence &sequence) |
Writes the key sequence to the stream. | |
QDataStream & | operator>> (QDataStream &stream, QKeySequence &sequence) |
Reads a key sequence from the stream into the key sequence. | |
The QKeySequence class encapsulates a key sequence as used by shortcuts.
\inmodule QtGui
In its most common form, a key sequence describes a combination of keys that must be used together to perform some action. Key sequences are used with QAction objects to specify which keyboard shortcuts can be used to trigger actions.
Key sequences can be constructed for use as keyboard shortcuts in three different ways:
\list
For example, \uicontrol{Ctrl P} might be a sequence used as a shortcut for printing a document, and can be specified in any of the following ways:
Note that, for letters, the case used in the specification string does not matter. In the above examples, the user does not need to hold down the \uicontrol{Shift} key to activate a shortcut specified with "Ctrl+P". However, for other keys, the use of \uicontrol{Shift} as an unspecified extra modifier key can lead to confusion for users of an application whose keyboards have different layouts to those used by the developers. See the \l{Keyboard Layout Issues} section below for more details.
It is preferable to use standard shortcuts where possible. When creating key sequences for non-standard shortcuts, you should use human-readable strings in preference to hard-coded integer values.
QKeySequence objects can be cast to a QString to obtain a human-readable translated version of the sequence. Similarly, the toString() function produces human-readable strings for use in menus. On Apple platforms, the appropriate symbols are used to describe keyboard shortcuts using special keys on the Macintosh keyboard.
An alternative way to specify hard-coded key codes is to use the Unicode code point of the character; for example, 'A' gives the same key sequence as Qt::Key_A.
Definition at line 34 of file qkeysequence.h.
Definition at line 183 of file qkeysequence.h.
\value NativeText The key sequence as a platform specific string.
This means that it will be shown translated and on Apple platforms it will resemble a key sequence from the menu bar. This enum is best used when you want to display the string to the user.
\value PortableText The key sequence is given in a "portable" format, suitable for reading and writing to a file. In many cases, it will look similar to the native text on Windows and X11.
Enumerator | |
---|---|
NativeText | |
PortableText |
Definition at line 114 of file qkeysequence.h.
\value NoMatch The key sequences are different; not even partially matching.
\value PartialMatch The key sequences match partially, but are not the same. \value ExactMatch The key sequences are the same.
Enumerator | |
---|---|
NoMatch | |
PartialMatch | |
ExactMatch |
Definition at line 133 of file qkeysequence.h.
This enum represent standard key bindings. They can be used to assign platform dependent keyboard shortcuts to a QAction.
Note that the key bindings are platform dependent. The currently bound shortcuts can be queried using keyBindings().
\value AddTab Add new tab. \value Back Navigate back. \value Backspace Delete previous character. \value Bold Bold text. \value Close Close document/tab. \value Copy Copy. \value Cut Cut. \value Delete Delete. \value DeleteEndOfLine Delete end of line. \value DeleteEndOfWord Delete word from the end of the cursor. \value DeleteStartOfWord Delete the beginning of a word up to the cursor. \value DeleteCompleteLine Delete the entire line. \value Find Find in document. \value FindNext Find next result. \value FindPrevious Find previous result. \value Forward Navigate forward. \value HelpContents Open help contents. \value InsertLineSeparator Insert a new line. \value InsertParagraphSeparator Insert a new paragraph. \value Italic Italic text. \value MoveToEndOfBlock Move cursor to end of block. This shortcut is only used on Apple platforms. \value MoveToEndOfDocument Move cursor to end of document. \value MoveToEndOfLine Move cursor to end of line. \value MoveToNextChar Move cursor to next character. \value MoveToNextLine Move cursor to next line. \value MoveToNextPage Move cursor to next page. \value MoveToNextWord Move cursor to next word. \value MoveToPreviousChar Move cursor to previous character. \value MoveToPreviousLine Move cursor to previous line. \value MoveToPreviousPage Move cursor to previous page. \value MoveToPreviousWord Move cursor to previous word. \value MoveToStartOfBlock Move cursor to start of a block. This shortcut is only used on Apple platforms. \value MoveToStartOfDocument Move cursor to start of document. \value MoveToStartOfLine Move cursor to start of line. \value New Create new document. \value NextChild Navigate to next tab or child window. \value Open Open document. \value Paste Paste. \value Preferences Open the preferences dialog. \value PreviousChild Navigate to previous tab or child window. \value Print Print document. \value Quit Quit the application. \value Redo Redo. \value Refresh Refresh or reload current document. \value Replace Find and replace. \value SaveAs Save document after prompting the user for a file name. \value Save Save document. \value SelectAll Select all text. \value Deselect Deselect text. Since 5.1 \value SelectEndOfBlock Extend selection to the end of a text block. This shortcut is only used on Apple platforms. \value SelectEndOfDocument Extend selection to end of document. \value SelectEndOfLine Extend selection to end of line. \value SelectNextChar Extend selection to next character. \value SelectNextLine Extend selection to next line. \value SelectNextPage Extend selection to next page. \value SelectNextWord Extend selection to next word. \value SelectPreviousChar Extend selection to previous character. \value SelectPreviousLine Extend selection to previous line. \value SelectPreviousPage Extend selection to previous page. \value SelectPreviousWord Extend selection to previous word. \value SelectStartOfBlock Extend selection to the start of a text block. This shortcut is only used on Apple platforms. \value SelectStartOfDocument Extend selection to start of document. \value SelectStartOfLine Extend selection to start of line. \value Underline Underline text. \value Undo Undo. \value UnknownKey Unbound key. \value WhatsThis Activate "what's this". \value ZoomIn Zoom in. \value ZoomOut Zoom out. \value FullScreen Toggle the window state to/from full screen. \value Cancel Cancel the current operation.
Definition at line 39 of file qkeysequence.h.
QKeySequence::QKeySequence | ( | ) |
Constructs an empty key sequence.
Definition at line 799 of file qkeysequence.cpp.
References QBasicAtomicInteger< T >::ref(), and QKeySequencePrivate::ref.
Referenced by fromString(), and mnemonic().
QKeySequence::QKeySequence | ( | const QString & | key, |
QKeySequence::SequenceFormat | format = NativeText |
||
) |
Creates a key sequence from the key string, based on format.
For example "Ctrl+O" gives CTRL+'O'. The strings "Ctrl", "Shift", "Alt" and "Meta" are recognized, as well as their translated equivalents in the "QShortcut" context (using QObject::tr()).
Up to four key codes may be entered by separating them with commas, e.g. "Alt+X,Ctrl+S,Q".
This constructor is typically used with \l{QObject::tr()}{tr}(), so that shortcut keys can be replaced in translations:
Note the "File|Open" translator comment. It is by no means necessary, but it provides some context for the human translator.
Definition at line 825 of file qkeysequence.cpp.
QKeySequence::QKeySequence | ( | int | k1, |
int | k2 = 0 , |
||
int | k3 = 0 , |
||
int | k4 = 0 |
||
) |
Constructs a key sequence with up to 4 keys k1, k2, k3 and k4.
The key codes are listed in Qt::Key and can be combined with modifiers (see Qt::KeyboardModifier) such as Qt::ShiftModifier, Qt::ControlModifier, Qt::AltModifier, or Qt::MetaModifier.
Definition at line 840 of file qkeysequence.cpp.
References k1, and QKeySequencePrivate::key.
QKeySequence::QKeySequence | ( | QKeyCombination | k1, |
QKeyCombination | k2 = QKeyCombination::fromCombined(0) , |
||
QKeyCombination | k3 = QKeyCombination::fromCombined(0) , |
||
QKeyCombination | k4 = QKeyCombination::fromCombined(0) |
||
) |
Constructs a key sequence with up to 4 keys k1, k2, k3 and k4.
Definition at line 855 of file qkeysequence.cpp.
QKeySequence::QKeySequence | ( | const QKeySequence & | keysequence | ) |
Copy constructor.
Makes a copy of keysequence.
Definition at line 863 of file qkeysequence.cpp.
References QBasicAtomicInteger< T >::ref(), and QKeySequencePrivate::ref.
QKeySequence::QKeySequence | ( | StandardKey | key | ) |
Constructs a QKeySequence object for the given key. The result will depend on the currently running platform.
The resulting object will be based on the first element in the list of key bindings for the key.
Definition at line 783 of file qkeysequence.cpp.
References QList< T >::first(), keyBindings(), QBasicAtomicInteger< T >::ref(), QKeySequencePrivate::ref, and QList< T >::size().
QKeySequence::~QKeySequence | ( | ) |
Destroys the key sequence.
Definition at line 886 of file qkeysequence.cpp.
References QBasicAtomicInteger< T >::deref(), and QKeySequencePrivate::ref.
int QKeySequence::count | ( | ) | const |
Returns the number of keys in the key sequence.
The maximum is 4.
Definition at line 911 of file qkeysequence.cpp.
References QKeySequencePrivate::key, and QKeySequencePrivate::MaxKeyCount.
Referenced by QDBusMenuItem::convertKeySequence(), QMacStyle::drawControl(), matches(), QAndroidInputContext::sendShortcut(), QKeySequenceEdit::setKeySequence(), QCocoaMenuItem::sync(), and toString().
|
inline |
Definition at line 184 of file qkeysequence.h.
References d.
|
static |
Return a QKeySequence from the string str based on format.
Definition at line 1535 of file qkeysequence.cpp.
References QKeySequence(), and str.
Referenced by QMacStyle::drawControl(), QWasmInputContext::inputStringChanged(), listFromString(), QQuickLabsPlatformMenuItem::sync(), valueToKeySequence(), and valueToKeySequences().
bool QKeySequence::isDetached | ( | ) | const |
Definition at line 1488 of file qkeysequence.cpp.
References QBasicAtomicInteger< T >::loadRelaxed(), and QKeySequencePrivate::ref.
bool QKeySequence::isEmpty | ( | ) | const |
Returns true
if the key sequence is empty; otherwise returns false.
Definition at line 921 of file qkeysequence.cpp.
References QKeySequencePrivate::key.
Referenced by QDBusMenuItem::QDBusMenuItem(), QDialogButtonBoxPrivate::createButton(), QWindowsMenuItem::formatDebug(), QGraphicsWidget::grabShortcut(), QMenu::initStyleOption(), QWindowsMenuItem::nativeText(), and QMenuPrivate::updateActionRects().
|
static |
Returns a list of key bindings for the given key. The result of calling this function will vary based on the target platform. The first element of the list indicates the primary shortcut for the given platform. If the result contains more than one result, these can be considered alternative shortcuts on the same platform for the given key.
Definition at line 878 of file qkeysequence.cpp.
References QGuiApplicationPrivate::platformTheme().
Referenced by QKeySequence(), QShortcut::QShortcut(), QShortcut::setKeys(), valueToKeySequence(), and valueToKeySequences().
|
static |
Return a list of QKeySequence from the string str based on format.
Definition at line 1548 of file qkeysequence.cpp.
References fromString(), QString::split(), and str.
|
static |
Return a string representation of list based on format.
Definition at line 1569 of file qkeysequence.cpp.
References list.
QKeySequence::SequenceMatch QKeySequence::matches | ( | const QKeySequence & | seq | ) | const |
Matches the sequence with seq.
Returns ExactMatch if successful, PartialMatch if seq matches incompletely, and NoMatch if the sequences have nothing in common. Returns NoMatch if seq is shorter.
Definition at line 1350 of file qkeysequence.cpp.
References count(), ExactMatch, i, match(), NoMatch, and PartialMatch.
|
static |
Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics are found.
For example, mnemonic("E&xit") returns {Qt::ALT+Qt::Key_X}, mnemonic("&Quit") returns
{ALT+Key_Q}, and mnemonic("Quit") returns an empty QKeySequence.
Definition at line 935 of file qkeysequence.cpp.
References QKeySequence(), Qt::ALT, QString::at(), QString::indexOf(), qPrintable, qt_sequence_no_mnemonics, qWarning, ret, QString::size(), text, and QChar::toUpper().
Referenced by QQuickAbstractButton::buttonChange(), QTabBar::insertTab(), QMenu::keyPressEvent(), QQuickMenu::keyPressEvent(), QTabBar::setTabText(), QAbstractButton::setText(), QGroupBox::setTitle(), QMenuBarPrivate::updateGeometries(), and QLabelPrivate::updateShortcut().
QKeySequence::operator QVariant | ( | ) | const |
Returns the key sequence as a QVariant.
Definition at line 1374 of file qkeysequence.cpp.
References QVariant::fromValue().
|
inline |
Returns true
if this key sequence is not equal to the other key sequence; otherwise returns false
.
Definition at line 156 of file qkeysequence.h.
References other().
bool QKeySequence::operator< | ( | const QKeySequence & | other | ) | const |
Provides an arbitrary comparison of this key sequence and other key sequence.
All that is guaranteed is that the operator returns false
if both key sequences are equal and that (ks1 < ks2) == !( ks2 < ks1) if the key sequences are not equal.
This function is useful in some circumstances, for example if you want to use QKeySequence objects as keys in a QMap.
Definition at line 1452 of file qkeysequence.cpp.
References QKeySequencePrivate::key, QKeySequencePrivate::MaxKeyCount, and other().
|
inline |
Returns true
if this key sequence is smaller or equal to the other key sequence; otherwise returns false
.
Definition at line 161 of file qkeysequence.h.
References other().
QKeySequence & QKeySequence::operator= | ( | const QKeySequence & | other | ) |
Move-assigns other to this QKeySequence instance.
Assignment operator.
Assigns the other key sequence to this object.
Definition at line 1394 of file qkeysequence.cpp.
References other(), and qAtomicAssign().
bool QKeySequence::operator== | ( | const QKeySequence & | other | ) | const |
Returns true
if this key sequence is equal to the other key sequence; otherwise returns false
.
Definition at line 1420 of file qkeysequence.cpp.
References QKeySequencePrivate::key, and other().
|
inline |
Returns true
if this key sequence is larger than the other key sequence; otherwise returns false
.
Definition at line 159 of file qkeysequence.h.
References other().
|
inline |
Returns true
if this key sequence is larger or equal to the other key sequence; otherwise returns false
.
Definition at line 163 of file qkeysequence.h.
References other().
QKeyCombination QKeySequence::operator[] | ( | uint | index | ) | const |
Returns a reference to the element at position index in the key sequence.
This can only be used to read an element.
Definition at line 1383 of file qkeysequence.cpp.
References QKeyCombination::fromCombined(), QKeySequencePrivate::key, QKeySequencePrivate::MaxKeyCount, and Q_ASSERT_X.
|
inlinenoexcept |
Swaps key sequence other with this key sequence. This operation is very fast and never fails.
Definition at line 153 of file qkeysequence.h.
References d, other(), and qt_ptr_swap().
QString QKeySequence::toString | ( | SequenceFormat | format = PortableText | ) | const |
Return a string representation of the key sequence, based on format.
For example, the value Qt::CTRL+Qt::Key_O results in "Ctrl+O". If the key sequence has multiple key codes, each is separated by commas in the string returned, such as "Alt+X, Ctrl+Y, Z". The strings, "Ctrl", "Shift", etc. are translated using QObject::tr() in the "QShortcut" context.
If the key sequence has no keys, an empty string is returned.
On Apple platforms, the string returned resembles the sequence that is shown in the menu bar if format is QKeySequence::NativeText; otherwise, the string uses the "portable" format, suitable for writing to a file.
Definition at line 1514 of file qkeysequence.cpp.
References count(), QKeySequencePrivate::encodeString(), i, QKeySequencePrivate::key, QString::size(), and QString::truncate().
Referenced by QMenu::initStyleOption(), QWindowsMenuItem::nativeText(), QQuickShortcut::nativeText(), QQuickShortcut::portableText(), QKeySequenceEditPrivate::resetState(), QCocoaMenuItem::sync(), QQuickLabsPlatformMenuItem::sync(), and QMenuPrivate::updateActionRects().
|
friend |
Writes the key sequence to the stream.
Definition at line 1594 of file qkeysequence.cpp.
|
related |
Writes the key sequence to the stream.
Definition at line 1594 of file qkeysequence.cpp.
|
friend |
Reads a key sequence from the stream into the key sequence.
Definition at line 1616 of file qkeysequence.cpp.
|
related |
Reads a key sequence from the stream into the key sequence.
Definition at line 1616 of file qkeysequence.cpp.
|
related |
Calculates the hash value of key, using seed to seed the calculation.
Definition at line 1435 of file qkeysequence.cpp.
|
friend |
Calculates the hash value of key, using seed to seed the calculation.
Definition at line 1435 of file qkeysequence.cpp.
|
friend |
Definition at line 180 of file qkeysequence.h.
|
friend |
Definition at line 179 of file qkeysequence.h.
|
related |
Specifies whether mnemonics for menu items, labels, etc., should be honored or not.
On Windows and X11, this feature is on by default; on \macos, it is off. When this feature is off (that is, when b is false), QKeySequence::mnemonic() always returns an empty string.
Definition at line 139 of file qkeysequence.cpp.
References qt_sequence_no_mnemonics.