QtBase  v6.3.1
Public Types | Public Member Functions | Static Public Member Functions | Friends | Related Functions | List of all members
QKeySequence Class Reference

The QKeySequence class encapsulates a key sequence as used by shortcuts. More...

#include <qkeysequence.h>

Public Types

enum  StandardKey {
  UnknownKey , HelpContents , WhatsThis , Open ,
  Close , Save , New , Delete ,
  Cut , Copy , Paste , Undo ,
  Redo , Back , Forward , Refresh ,
  ZoomIn , ZoomOut , Print , AddTab ,
  NextChild , PreviousChild , Find , FindNext ,
  FindPrevious , Replace , SelectAll , Bold ,
  Italic , Underline , MoveToNextChar , MoveToPreviousChar ,
  MoveToNextWord , MoveToPreviousWord , MoveToNextLine , MoveToPreviousLine ,
  MoveToNextPage , MoveToPreviousPage , MoveToStartOfLine , MoveToEndOfLine ,
  MoveToStartOfBlock , MoveToEndOfBlock , MoveToStartOfDocument , MoveToEndOfDocument ,
  SelectNextChar , SelectPreviousChar , SelectNextWord , SelectPreviousWord ,
  SelectNextLine , SelectPreviousLine , SelectNextPage , SelectPreviousPage ,
  SelectStartOfLine , SelectEndOfLine , SelectStartOfBlock , SelectEndOfBlock ,
  SelectStartOfDocument , SelectEndOfDocument , DeleteStartOfWord , DeleteEndOfWord ,
  DeleteEndOfLine , InsertParagraphSeparator , InsertLineSeparator , SaveAs ,
  Preferences , Quit , FullScreen , Deselect ,
  DeleteCompleteLine , Backspace , Cancel
}
 
enum  SequenceFormat { NativeText , PortableText }
 
enum  SequenceMatch { NoMatch , PartialMatch , ExactMatch }
 
typedef QKeySequencePrivateDataPtr
 

Public Member Functions

 QKeySequence ()
 
 QKeySequence (const QString &key, SequenceFormat format=NativeText)
 
 QKeySequence (int k1, int k2=0, int k3=0, int k4=0)
 
 QKeySequence (QKeyCombination k1, QKeyCombination k2=QKeyCombination::fromCombined(0), QKeyCombination k3=QKeyCombination::fromCombined(0), QKeyCombination k4=QKeyCombination::fromCombined(0))
 
 QKeySequence (const QKeySequence &ks)
 
 QKeySequence (StandardKey key)
 
 ~QKeySequence ()
 
int count () const
 
bool isEmpty () const
 
QString toString (SequenceFormat format=PortableText) const
 
SequenceMatch matches (const QKeySequence &seq) const
 
 operator QVariant () const
 
QKeyCombination operator[] (uint i) const
 
QKeySequenceoperator= (const QKeySequence &other)
 
void swap (QKeySequence &other) noexcept
 
bool operator== (const QKeySequence &other) const
 
bool operator!= (const QKeySequence &other) const
 
bool operator< (const QKeySequence &ks) const
 
bool operator> (const QKeySequence &other) const
 
bool operator<= (const QKeySequence &other) const
 
bool operator>= (const QKeySequence &other) const
 
bool isDetached () const
 
DataPtrdata_ptr ()
 

Static Public Member Functions

static QKeySequence fromString (const QString &str, SequenceFormat format=PortableText)
 
static QList< QKeySequencelistFromString (const QString &str, SequenceFormat format=PortableText)
 
static QString listToString (const QList< QKeySequence > &list, SequenceFormat format=PortableText)
 
static QKeySequence mnemonic (const QString &text)
 
static QList< QKeySequencekeyBindings (StandardKey key)
 

Friends

class QShortcutMap
 
class QShortcut
 
Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &in, const QKeySequence &ks)
 
Q_GUI_EXPORT QDataStreamoperator>> (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)
 
QDataStreamoperator<< (QDataStream &stream, const QKeySequence &sequence)
 
QDataStreamoperator>> (QDataStream &stream, QKeySequence &sequence)
 

Detailed Description

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.

Note
On \macos, references to "Ctrl", Qt::CTRL, Qt::Key_Control and Qt::ControlModifier correspond to the \uicontrol Command keys on the Macintosh keyboard, and references to "Meta", Qt::META, Qt::Key_Meta and Qt::MetaModifier correspond to the \uicontrol Control keys. Developers on \macos can use the same shortcut descriptions across all platforms, and their applications will automatically work as expected on \macos.

Definition at line 70 of file qkeysequence.h.

Member Typedef Documentation

◆ DataPtr

Definition at line 219 of file qkeysequence.h.

Member Enumeration Documentation

◆ SequenceFormat

\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.

◆ SequenceMatch

\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.

◆ StandardKey

Since
4.2

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.

Enumerator
UnknownKey 
HelpContents 
WhatsThis 
Open 
Close 
Save 
New 
Delete 
Cut 
Copy 
Paste 
Undo 
Redo 
Back 
Forward 
Refresh 
ZoomIn 
ZoomOut 
Print 
AddTab 
NextChild 
PreviousChild 
Find 
FindNext 
FindPrevious 
Replace 
SelectAll 
Bold 
Italic 
Underline 
MoveToNextChar 
MoveToPreviousChar 
MoveToNextWord 
MoveToPreviousWord 
MoveToNextLine 
MoveToPreviousLine 
MoveToNextPage 
MoveToPreviousPage 
MoveToStartOfLine 
MoveToEndOfLine 
MoveToStartOfBlock 
MoveToEndOfBlock 
MoveToStartOfDocument 
MoveToEndOfDocument 
SelectNextChar 
SelectPreviousChar 
SelectNextWord 
SelectPreviousWord 
SelectNextLine 
SelectPreviousLine 
SelectNextPage 
SelectPreviousPage 
SelectStartOfLine 
SelectEndOfLine 
SelectStartOfBlock 
SelectEndOfBlock 
SelectStartOfDocument 
SelectEndOfDocument 
DeleteStartOfWord 
DeleteEndOfWord 
DeleteEndOfLine 
InsertParagraphSeparator 
InsertLineSeparator 
SaveAs 
Preferences 
Quit 
FullScreen 
Deselect 
DeleteCompleteLine 
Backspace 
Cancel 

Definition at line 75 of file qkeysequence.h.

Constructor & Destructor Documentation

◆ QKeySequence() [1/6]

QKeySequence::QKeySequence ( )

Constructs an empty key sequence.

Definition at line 820 of file qkeysequence.cpp.

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

◆ QKeySequence() [2/6]

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() [3/6]

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() [4/6]

Constructs a key sequence with up to 4 keys k1, k2, k3 and k4.

See also
QKeyCombination

Definition at line 876 of file qkeysequence.cpp.

◆ QKeySequence() [5/6]

QKeySequence::QKeySequence ( const QKeySequence keysequence)

Copy constructor. Makes a copy of keysequence.

Definition at line 884 of file qkeysequence.cpp.

Here is the call graph for this function:

◆ QKeySequence() [6/6]

QKeySequence::QKeySequence ( StandardKey  key)
Since
4.2

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.

Here is the call graph for this function:

◆ ~QKeySequence()

QKeySequence::~QKeySequence ( )

Destroys the key sequence.

Definition at line 907 of file qkeysequence.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ count()

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.

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

◆ data_ptr()

DataPtr & QKeySequence::data_ptr ( )
inline

Definition at line 220 of file qkeysequence.h.

◆ fromString()

QKeySequence QKeySequence::fromString ( const QString str,
SequenceFormat  format = PortableText 
)
static
Since
4.1

Return a QKeySequence from the string str based on format.

See also
toString()

Definition at line 1560 of file qkeysequence.cpp.

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

◆ isDetached()

bool QKeySequence::isDetached ( ) const

Definition at line 1513 of file qkeysequence.cpp.

Here is the call graph for this function:

◆ isEmpty()

bool QKeySequence::isEmpty ( ) const

Returns true if the key sequence is empty; otherwise returns false.

Definition at line 942 of file qkeysequence.cpp.

Here is the caller graph for this function:

◆ keyBindings()

QList< QKeySequence > QKeySequence::keyBindings ( StandardKey  key)
static
Since
4.2

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.

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

◆ listFromString()

QList< QKeySequence > QKeySequence::listFromString ( const QString str,
SequenceFormat  format = PortableText 
)
static
Since
5.1

Return a list of QKeySequence from the string str based on format.

See also
fromString()
listToString()

Definition at line 1573 of file qkeysequence.cpp.

Here is the call graph for this function:

◆ listToString()

QString QKeySequence::listToString ( const QList< QKeySequence > &  list,
SequenceFormat  format = PortableText 
)
static
Since
5.1

Return a string representation of list based on format.

See also
toString()
listFromString()

Definition at line 1594 of file qkeysequence.cpp.

◆ matches()

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.

Here is the call graph for this function:

◆ mnemonic()

QKeySequence QKeySequence::mnemonic ( const QString text)
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.

Here is the caller graph for this function:

◆ operator QVariant()

QKeySequence::operator QVariant ( ) const

Returns the key sequence as a QVariant

Definition at line 1400 of file qkeysequence.cpp.

Here is the call graph for this function:

◆ operator!=()

bool QKeySequence::operator!= ( const QKeySequence other) const
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.

Here is the call graph for this function:

◆ operator<()

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.

See also
operator==(), operator!=(), operator>(), operator<=(), operator>=()

Definition at line 1477 of file qkeysequence.cpp.

Here is the call graph for this function:

◆ operator<=()

bool QKeySequence::operator<= ( const QKeySequence other) const
inline

Returns true if this key sequence is smaller or equal to the other key sequence; otherwise returns false.

See also
operator==(), operator!=(), operator<(), operator>(), operator>=()

Definition at line 197 of file qkeysequence.h.

Here is the call graph for this function:

◆ operator=()

QKeySequence & QKeySequence::operator= ( const QKeySequence other)

Move-assigns other to this QKeySequence instance.

Since
5.2

Assignment operator. Assigns the other key sequence to this object.

Definition at line 1420 of file qkeysequence.cpp.

Here is the call graph for this function:

◆ operator==()

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.

Here is the call graph for this function:

◆ operator>()

bool QKeySequence::operator> ( const QKeySequence other) const
inline

Returns true if this key sequence is larger than the other key sequence; otherwise returns false.

See also
operator==(), operator!=(), operator<(), operator<=(), operator>=()

Definition at line 195 of file qkeysequence.h.

Here is the call graph for this function:

◆ operator>=()

bool QKeySequence::operator>= ( const QKeySequence other) const
inline

Returns true if this key sequence is larger or equal to the other key sequence; otherwise returns false.

See also
operator==(), operator!=(), operator<(), operator>(), operator<=()

Definition at line 199 of file qkeysequence.h.

Here is the call graph for this function:

◆ operator[]()

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.

Here is the call graph for this function:

◆ swap()

void QKeySequence::swap ( QKeySequence other)
inlinenoexcept
Since
4.8

Swaps key sequence other with this key sequence. This operation is very fast and never fails.

Definition at line 189 of file qkeysequence.h.

Here is the call graph for this function:

◆ toString()

QString QKeySequence::toString ( SequenceFormat  format = PortableText) const
Since
4.1

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.

See also
fromString()

Definition at line 1539 of file qkeysequence.cpp.

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

Friends And Related Function Documentation

◆ operator<< [1/2]

QDataStream & operator<< ( QDataStream stream,
const QKeySequence sequence 
)
friend

Writes the key sequence to the stream.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 1619 of file qkeysequence.cpp.

◆ operator<<() [2/2]

QDataStream & operator<< ( QDataStream stream,
const QKeySequence sequence 
)
related

Writes the key sequence to the stream.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 1619 of file qkeysequence.cpp.

◆ operator>> [1/2]

QDataStream & operator>> ( QDataStream stream,
QKeySequence sequence 
)
friend

Reads a key sequence from the stream into the key sequence.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 1641 of file qkeysequence.cpp.

◆ operator>>() [2/2]

QDataStream & operator>> ( QDataStream stream,
QKeySequence sequence 
)
related

Reads a key sequence from the stream into the key sequence.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 1641 of file qkeysequence.cpp.

◆ qHash

Q_GUI_EXPORT size_t qHash ( const QKeySequence key,
size_t  seed 
)
friend
Since
5.6

Calculates the hash value of key, using seed to seed the calculation.

Definition at line 1460 of file qkeysequence.cpp.

◆ QShortcut

friend class QShortcut
friend

Definition at line 216 of file qkeysequence.h.

◆ QShortcutMap

friend class QShortcutMap
friend

Definition at line 215 of file qkeysequence.h.

◆ qt_set_sequence_auto_mnemonic()

void qt_set_sequence_auto_mnemonic ( bool  b)
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.

Note
This function is not declared in any of Qt's header files. To use it in your application, declare the function prototype before calling it.
See also
QShortcut

Definition at line 165 of file qkeysequence.cpp.


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