QtBase
v6.3.1
|
The QKeySequence class encapsulates a key sequence as used by shortcuts. More...
#include <qkeysequence.h>
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) |
static QList< QKeySequence > | keyBindings (StandardKey key) |
Friends | |
class | QShortcutMap |
class | QShortcut |
Q_GUI_EXPORT QDataStream & | operator<< (QDataStream &in, const QKeySequence &ks) |
Q_GUI_EXPORT QDataStream & | operator>> (QDataStream &in, QKeySequence &ks) |
Q_GUI_EXPORT size_t | qHash (const QKeySequence &key, size_t seed) noexcept |
Related Functions | |
(Note that these are not member functions.) | |
void | qt_set_sequence_auto_mnemonic (bool b) |
QDataStream & | operator<< (QDataStream &stream, const QKeySequence &sequence) |
QDataStream & | operator>> (QDataStream &stream, QKeySequence &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 \macos, 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 70 of file qkeysequence.h.
Definition at line 219 of file qkeysequence.h.
\value NativeText The key sequence as a platform specific string. This means that it will be shown translated and on the Mac 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 150 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 169 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 the \macos. \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 \macos. \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 \macos. \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 \macos. \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 75 of file qkeysequence.h.
QKeySequence::QKeySequence | ( | ) |
Constructs an empty key sequence.
Definition at line 820 of file qkeysequence.cpp.
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 846 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::Modifier) such as Qt::SHIFT, Qt::CTRL, Qt::ALT, or Qt::META.
Definition at line 861 of file qkeysequence.cpp.
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 876 of file qkeysequence.cpp.
QKeySequence::QKeySequence | ( | const QKeySequence & | keysequence | ) |
Copy constructor. Makes a copy of keysequence.
Definition at line 884 of file qkeysequence.cpp.
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 804 of file qkeysequence.cpp.
QKeySequence::~QKeySequence | ( | ) |
Destroys the key sequence.
Definition at line 907 of file qkeysequence.cpp.
int QKeySequence::count | ( | ) | const |
Returns the number of keys in the key sequence. The maximum is 4.
Definition at line 932 of file qkeysequence.cpp.
|
inline |
Definition at line 220 of file qkeysequence.h.
|
static |
Return a QKeySequence from the string str based on format.
Definition at line 1560 of file qkeysequence.cpp.
bool QKeySequence::isDetached | ( | ) | const |
bool QKeySequence::isEmpty | ( | ) | const |
Returns true
if the key sequence is empty; otherwise returns false.
Definition at line 942 of file qkeysequence.cpp.
|
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 899 of file qkeysequence.cpp.
|
static |
Return a list of QKeySequence from the string str based on format.
Definition at line 1573 of file qkeysequence.cpp.
|
static |
Return a string representation of list based on format.
Definition at line 1594 of file qkeysequence.cpp.
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 1376 of file qkeysequence.cpp.
|
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.
We provide a \l{accelerators.html}{list of common mnemonics} in English. At the time of writing, Microsoft and Open Group do not appear to have issued equivalent recommendations for other languages.
Definition at line 961 of file qkeysequence.cpp.
QKeySequence::operator QVariant | ( | ) | const |
Returns the key sequence as a QVariant
Definition at line 1400 of file qkeysequence.cpp.
|
inline |
Returns true
if this key sequence is not equal to the other key sequence; otherwise returns false
.
Definition at line 192 of file qkeysequence.h.
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 1477 of file qkeysequence.cpp.
|
inline |
Returns true
if this key sequence is smaller or equal to the other key sequence; otherwise returns false
.
Definition at line 197 of file qkeysequence.h.
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 1420 of file qkeysequence.cpp.
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 1446 of file qkeysequence.cpp.
|
inline |
Returns true
if this key sequence is larger than the other key sequence; otherwise returns false
.
Definition at line 195 of file qkeysequence.h.
|
inline |
Returns true
if this key sequence is larger or equal to the other key sequence; otherwise returns false
.
Definition at line 199 of file qkeysequence.h.
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 1409 of file qkeysequence.cpp.
|
inlinenoexcept |
Swaps key sequence other with this key sequence. This operation is very fast and never fails.
Definition at line 189 of file qkeysequence.h.
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 \macos, 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 1539 of file qkeysequence.cpp.
|
friend |
Writes the key sequence to the stream.
Definition at line 1619 of file qkeysequence.cpp.
|
related |
Writes the key sequence to the stream.
Definition at line 1619 of file qkeysequence.cpp.
|
friend |
Reads a key sequence from the stream into the key sequence.
Definition at line 1641 of file qkeysequence.cpp.
|
related |
Reads a key sequence from the stream into the key sequence.
Definition at line 1641 of file qkeysequence.cpp.
|
friend |
Calculates the hash value of key, using seed to seed the calculation.
Definition at line 1460 of file qkeysequence.cpp.
|
friend |
Definition at line 216 of file qkeysequence.h.
|
friend |
Definition at line 215 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 165 of file qkeysequence.cpp.