<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qshortcut.cpp --> <title>QShortcut Class | Qt Widgets 5.12.2</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-module.html">C++ Classes</a></td><td >QShortcut</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.2 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#properties">Properties</a></li> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#signals">Signals</a></li> <li class="level1"><a href="#static-public-members">Static Public Members</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QShortcut Class</h1> <!-- $$$QShortcut-brief --> <p>The <a href="qshortcut.html">QShortcut</a> class is used to create keyboard shortcuts. <a href="#details">More...</a></p> <!-- @@@QShortcut --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QShortcut></span> </td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += widgets</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> <a href="../qtcore/qobject.html">QObject</a></td></tr></table></div><ul> <li><a href="qshortcut-members.html">List of all members, including inherited members</a></li> <li><a href="qshortcut-obsolete.html">Obsolete members</a></li> </ul> <a name="properties"></a> <h2 id="properties">Properties</h2> <div class="table"><table class="propsummary"> <tr><td class="topAlign"><ul> <li class="fn"><b><a href="qshortcut.html#autoRepeat-prop">autoRepeat</a></b> : bool</li> <li class="fn"><b><a href="qshortcut.html#context-prop">context</a></b> : Qt::ShortcutContext</li> <li class="fn"><b><a href="qshortcut.html#enabled-prop">enabled</a></b> : bool</li> </ul></td><td class="topAlign"><ul> <li class="fn"><b><a href="qshortcut.html#key-prop">key</a></b> : QKeySequence</li> <li class="fn"><b><a href="qshortcut.html#whatsThis-prop">whatsThis</a></b> : QString</li> </ul> </td></tr> </table></div> <ul> <li class="fn">1 property inherited from <a href="../qtcore/qobject.html#properties">QObject</a></li> </ul> <a name="public-functions"></a> <h2 id="public-functions">Public Functions</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#QShortcut">QShortcut</a></b>(QWidget *<i>parent</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#QShortcut-1">QShortcut</a></b>(const QKeySequence &<i>key</i>, QWidget *<i>parent</i>, const char *<i>member</i> = nullptr, const char *<i>ambiguousMember</i> = nullptr, Qt::ShortcutContext <i>context</i> = Qt::WindowShortcut)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#dtor.QShortcut">~QShortcut</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#autoRepeat-prop">autoRepeat</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> Qt::ShortcutContext </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#context-prop">context</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#id">id</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#enabled-prop">isEnabled</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QKeySequence </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#key-prop">key</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QWidget *</td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#parentWidget">parentWidget</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#autoRepeat-prop">setAutoRepeat</a></b>(bool <i>on</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#context-prop">setContext</a></b>(Qt::ShortcutContext <i>context</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#enabled-prop">setEnabled</a></b>(bool <i>enable</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#key-prop">setKey</a></b>(const QKeySequence &<i>key</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#whatsThis-prop">setWhatsThis</a></b>(const QString &<i>text</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#whatsThis-prop">whatsThis</a></b>() const</td></tr> </table></div> <ul> <li class="fn">31 public functions inherited from <a href="../qtcore/qobject.html#public-functions">QObject</a></li> </ul> <a name="signals"></a> <h2 id="signals">Signals</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#activated">activated</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#activatedAmbiguously">activatedAmbiguously</a></b>()</td></tr> </table></div> <ul> <li class="fn">2 signals inherited from <a href="../qtcore/qobject.html#signals">QObject</a></li> </ul> <a name="static-public-members"></a> <h2 id="static-public-members">Static Public Members</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> const QMetaObject </td><td class="memItemRight bottomAlign"><b><a href="qshortcut.html#staticMetaObject-var">staticMetaObject</a></b></td></tr> </table></div> <ul> <li class="fn">9 static public members inherited from <a href="../qtcore/qobject.html#static-public-members">QObject</a></li> </ul> <h3>Additional Inherited Members</h3> <ul> <li class="fn">1 public slot inherited from <a href="../qtcore/qobject.html#public-slots">QObject</a></li> <li class="fn">9 protected functions inherited from <a href="../qtcore/qobject.html#protected-functions">QObject</a></li> </ul> <a name="details"></a> <!-- $$$QShortcut-description --> <div class="descr"> <h2 id="details">Detailed Description</h2> <p>The <a href="qshortcut.html">QShortcut</a> class is used to create keyboard shortcuts.</p> <p>The <a href="qshortcut.html">QShortcut</a> class provides a way of connecting keyboard shortcuts to Qt's <a href="../qtcore/signalsandslots.html">signals and slots</a> 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 <b>Shift</b>, <b>Ctrl</b>, and <b>Alt</b>.</p> <a name="mnemonic"></a><p>On certain widgets, using '&' in front of a character will automatically create a mnemonic (a shortcut) for that character, e.g. "E&xit" will create the shortcut <b>Alt+X</b> (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 <b>Alt</b> key, but this is a setting the user can change. On Mac, shortcuts are disabled by default. Call <a href="../qtgui/qkeysequence.html#qt_set_sequence_auto_mnemonic">qt_set_sequence_auto_mnemonic</a>() to enable them. However, because mnemonic shortcuts do not fit in with Aqua's guidelines, Qt will not show the shortcut character underlined.</p> <p>For applications that use menus, it may be more convenient to use the convenience functions provided in the <a href="qmenu.html">QMenu</a> class to assign keyboard shortcuts to menu items as they are created. Alternatively, shortcuts may be associated with other types of actions in the <a href="qaction.html">QAction</a> class.</p> <p>The simplest way to create a shortcut for a particular widget is to construct the shortcut with a key sequence. For example:</p> <pre class="cpp"> shortcut <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qshortcut.html#QShortcut">QShortcut</a></span>(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span>(tr(<span class="string">"Ctrl+O"</span><span class="operator">,</span> <span class="string">"File|Open"</span>))<span class="operator">,</span> parent); </pre> <p>When the user types the <a href="../qtgui/qkeysequence.html">key sequence</a> for a given shortcut, the shortcut's <a href="qshortcut.html#activated">activated</a>() signal is emitted. (In the case of ambiguity, the <a href="qshortcut.html#activatedAmbiguously">activatedAmbiguously</a>() signal is emitted.) A shortcut is "listened for" by Qt's event loop when the shortcut's parent widget is receiving events.</p> <p>A shortcut's key sequence can be set with <a href="qshortcut.html#key-prop">setKey</a>() and retrieved with <a href="qshortcut.html#key-prop">key</a>(). A shortcut can be enabled or disabled with <a href="qshortcut.html#enabled-prop">setEnabled</a>(), and can have "What's This?" help text set with <a href="qshortcut.html#whatsThis-prop">setWhatsThis</a>().</p> </div> <p><b>See also </b><a href="../qtgui/qshortcutevent.html">QShortcutEvent</a>, <a href="../qtgui/qkeysequence.html">QKeySequence</a>, and <a href="qaction.html">QAction</a>.</p> <!-- @@@QShortcut --> <div class="prop"> <h2>Property Documentation</h2> <!-- $$$autoRepeat-prop$$$autoRepeat$$$setAutoRepeatbool --> <h3 class="fn" id="autoRepeat-prop"><a name="autoRepeat-prop"></a><span class="name">autoRepeat</span> : <span class="type">bool</span></h3> <p>This property holds whether the shortcut can auto repeat</p> <p>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.</p> <p>This property was introduced in Qt 4.2.</p> <p><b>Access functions:</b></p> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft topAlign rightAlign"> bool </td><td class="memItemRight bottomAlign"><span class="name"><b>autoRepeat</b></span>() const</td></tr> <tr><td class="memItemLeft topAlign rightAlign"> void </td><td class="memItemRight bottomAlign"><span class="name"><b>setAutoRepeat</b></span>(bool <i>on</i>)</td></tr> </table></div> <!-- @@@autoRepeat --> <!-- $$$context-prop$$$context$$$setContextQt::ShortcutContext --> <h3 class="fn" id="context-prop"><a name="context-prop"></a><span class="name">context</span> : <span class="type"><a href="../qtcore/qt.html#ShortcutContext-enum">Qt::ShortcutContext</a></span></h3> <p>This property holds the context in which the shortcut is valid</p> <p>A shortcut's context decides in which circumstances a shortcut is allowed to be triggered. The normal context is <a href="../qtcore/qt.html#ShortcutContext-enum">Qt::WindowShortcut</a>, which allows the shortcut to trigger if the parent (the widget containing the shortcut) is a subwidget of the active top-level window.</p> <p>By default, this property is set to <a href="../qtcore/qt.html#ShortcutContext-enum">Qt::WindowShortcut</a>.</p> <p><b>Access functions:</b></p> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft topAlign rightAlign"> Qt::ShortcutContext </td><td class="memItemRight bottomAlign"><span class="name"><b>context</b></span>() const</td></tr> <tr><td class="memItemLeft topAlign rightAlign"> void </td><td class="memItemRight bottomAlign"><span class="name"><b>setContext</b></span>(Qt::ShortcutContext <i>context</i>)</td></tr> </table></div> <!-- @@@context --> <!-- $$$enabled-prop$$$isEnabled$$$setEnabledbool --> <h3 class="fn" id="enabled-prop"><a name="enabled-prop"></a><span class="name">enabled</span> : <span class="type">bool</span></h3> <p>This property holds whether the shortcut is enabled</p> <p>An enabled shortcut emits the <a href="qshortcut.html#activated">activated</a>() or <a href="qshortcut.html#activatedAmbiguously">activatedAmbiguously</a>() signal when a <a href="../qtgui/qshortcutevent.html">QShortcutEvent</a> occurs that matches the shortcut's <a href="qshortcut.html#key-prop">key</a>() sequence.</p> <p>If the application is in <code>WhatsThis</code> mode the shortcut will not emit the signals, but will show the "What's This?" text instead.</p> <p>By default, this property is <code>true</code>.</p> <p><b>Access functions:</b></p> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft topAlign rightAlign"> bool </td><td class="memItemRight bottomAlign"><span class="name"><b>isEnabled</b></span>() const</td></tr> <tr><td class="memItemLeft topAlign rightAlign"> void </td><td class="memItemRight bottomAlign"><span class="name"><b>setEnabled</b></span>(bool <i>enable</i>)</td></tr> </table></div> <p><b>See also </b><a href="qshortcut.html#whatsThis-prop">whatsThis</a>.</p> <!-- @@@enabled --> <!-- $$$key-prop$$$key$$$setKeyconstQKeySequence& --> <h3 class="fn" id="key-prop"><a name="key-prop"></a><span class="name">key</span> : <span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span></h3> <p>This property holds the shortcut's key sequence</p> <p>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:</p> <pre class="cpp"> setKey(<span class="number">0</span>); <span class="comment">// no signal emitted</span> setKey(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span>()); <span class="comment">// no signal emitted</span> setKey(<span class="number">0x3b1</span>); <span class="comment">// Greek letter alpha</span> setKey(<span class="type">Qt</span><span class="operator">::</span>Key_D); <span class="comment">// 'd', e.g. to delete</span> setKey(<span class="char">'q'</span>); <span class="comment">// 'q', e.g. to quit</span> setKey(<span class="type">Qt</span><span class="operator">::</span>CTRL <span class="operator">+</span> <span class="type">Qt</span><span class="operator">::</span>Key_P); <span class="comment">// Ctrl+P, e.g. to print document</span> setKey(<span class="string">"Ctrl+P"</span>); <span class="comment">// Ctrl+P, e.g. to print document</span> </pre> <p>By default, this property contains an empty key sequence.</p> <p><b>Access functions:</b></p> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft topAlign rightAlign"> QKeySequence </td><td class="memItemRight bottomAlign"><span class="name"><b>key</b></span>() const</td></tr> <tr><td class="memItemLeft topAlign rightAlign"> void </td><td class="memItemRight bottomAlign"><span class="name"><b>setKey</b></span>(const QKeySequence &<i>key</i>)</td></tr> </table></div> <!-- @@@key --> <!-- $$$whatsThis-prop$$$whatsThis$$$setWhatsThisconstQString& --> <h3 class="fn" id="whatsThis-prop"><a name="whatsThis-prop"></a><span class="name">whatsThis</span> : <span class="type"><a href="../qtcore/qstring.html">QString</a></span></h3> <p>This property holds the shortcut's "What's This?" help text</p> <p>The text will be shown when the application is in "What's This?" mode and the user types the shortcut <a href="qshortcut.html#key-prop">key</a>() sequence.</p> <p>To set "What's This?" help on a menu item (with or without a shortcut key), set the help on the item's action.</p> <p>By default, this property contains an empty string.</p> <p><b>Access functions:</b></p> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft topAlign rightAlign"> QString </td><td class="memItemRight bottomAlign"><span class="name"><b>whatsThis</b></span>() const</td></tr> <tr><td class="memItemLeft topAlign rightAlign"> void </td><td class="memItemRight bottomAlign"><span class="name"><b>setWhatsThis</b></span>(const QString &<i>text</i>)</td></tr> </table></div> <p><b>See also </b><a href="qwhatsthis.html#inWhatsThisMode">QWhatsThis::inWhatsThisMode</a>() and <a href="qaction.html#whatsThis-prop">QAction::setWhatsThis</a>().</p> <!-- @@@whatsThis --> </div> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$QShortcut[overload1]$$$QShortcutQWidget* --> <h3 class="fn" id="QShortcut"><a name="QShortcut"></a>QShortcut::<span class="name">QShortcut</span>(<span class="type"><a href="qwidget.html">QWidget</a></span> *<i>parent</i>)</h3> <p>Constructs a <a href="qshortcut.html">QShortcut</a> object for the <i>parent</i> widget. Since no shortcut key sequence is specified, the shortcut will not emit any signals.</p> <p><b>See also </b><a href="qshortcut.html#key-prop">setKey</a>().</p> <!-- @@@QShortcut --> <!-- $$$QShortcut$$$QShortcutconstQKeySequence&QWidget*constchar*constchar*Qt::ShortcutContext --> <h3 class="fn" id="QShortcut-1"><a name="QShortcut-1"></a>QShortcut::<span class="name">QShortcut</span>(const <span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span> &<i>key</i>, <span class="type"><a href="qwidget.html">QWidget</a></span> *<i>parent</i>, const <span class="type">char</span> *<i>member</i> = nullptr, const <span class="type">char</span> *<i>ambiguousMember</i> = nullptr, <span class="type"><a href="../qtcore/qt.html#ShortcutContext-enum">Qt::ShortcutContext</a></span> <i>context</i> = Qt::WindowShortcut)</h3> <p>Constructs a <a href="qshortcut.html">QShortcut</a> object for the <i>parent</i> widget. The shortcut operates on its parent, listening for <a href="../qtgui/qshortcutevent.html">QShortcutEvent</a>s that match the <i>key</i> sequence. Depending on the ambiguity of the event, the shortcut will call the <i>member</i> function, or the <i>ambiguousMember</i> function, if the key press was in the shortcut's <i>context</i>.</p> <!-- @@@QShortcut --> <!-- $$$~QShortcut[overload1]$$$~QShortcut --> <h3 class="fn" id="dtor.QShortcut"><a name="dtor.QShortcut"></a><code>[virtual] </code>QShortcut::<span class="name">~QShortcut</span>()</h3> <p>Destroys the shortcut.</p> <!-- @@@~QShortcut --> <!-- $$$activated[overload1]$$$activated --> <h3 class="fn" id="activated"><a name="activated"></a><code>[signal] </code><span class="type">void</span> QShortcut::<span class="name">activated</span>()</h3> <p>This signal is emitted when the user types the shortcut's key sequence.</p> <p><b>See also </b><a href="qshortcut.html#activatedAmbiguously">activatedAmbiguously</a>().</p> <!-- @@@activated --> <!-- $$$activatedAmbiguously[overload1]$$$activatedAmbiguously --> <h3 class="fn" id="activatedAmbiguously"><a name="activatedAmbiguously"></a><code>[signal] </code><span class="type">void</span> QShortcut::<span class="name">activatedAmbiguously</span>()</h3> <p>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.</p> <p>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 <a href="qshortcut.html#activated">activated</a>() signal is not emitted in this case.</p> <p><b>See also </b><a href="qshortcut.html#activated">activated</a>().</p> <!-- @@@activatedAmbiguously --> <!-- $$$id[overload1]$$$id --> <h3 class="fn" id="id"><a name="id"></a><span class="type">int</span> QShortcut::<span class="name">id</span>() const</h3> <p>Returns the shortcut's ID.</p> <p><b>See also </b><a href="../qtgui/qshortcutevent.html#shortcutId">QShortcutEvent::shortcutId</a>().</p> <!-- @@@id --> <!-- $$$parentWidget[overload1]$$$parentWidget --> <h3 class="fn" id="parentWidget"><a name="parentWidget"></a><span class="type"><a href="qwidget.html">QWidget</a></span> *QShortcut::<span class="name">parentWidget</span>() const</h3> <p>Returns the shortcut's parent widget.</p> <!-- @@@parentWidget --> </div> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>