The QShortcut 類用於創建鍵盤快捷方式。 更多...
| 頭: | #include <QShortcut> |
| qmake: | QT += widgets |
| 繼承: | QObject |
|
| QShortcut (QWidget * parent ) | |
| QShortcut (const QKeySequence & key , QWidget * parent , const char * member = Q_NULLPTR, const char * ambiguousMember = Q_NULLPTR, Qt::ShortcutContext context = Qt::WindowShortcut) | |
| ~QShortcut () | |
| bool | autoRepeat () const |
| Qt::ShortcutContext | context () const |
| int | id () const |
| bool | isEnabled () const |
| QKeySequence | key () const |
| QWidget * | parentWidget () 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 |
| void | activated () |
| void | activatedAmbiguously () |
The QShortcut 類用於創建鍵盤快捷方式。
The QShortcut class provides a way of connecting keyboard shortcuts to Qt's 信號/槽 機製,以便可以通報對象當執行快捷方式時。可以將快捷鍵方式設為包含描述鍵盤快捷方式的所有必要鍵按下,包括修飾符鍵的狀態,譬如 Shift , Ctrl ,和 Alt .
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 Alt+X (使用 && 能顯示實際和號)。Widget 可以按給定快捷方式消耗並履行動作。在 X11,和號不顯示且字符會加下劃綫。在 Windows,快捷方式通常不顯示直到用戶按下 Alt 鍵,但這是用戶可以改變的設定。在 Mac,默認禁用快捷方式。調用 qt_set_sequence_auto_mnemonic () 以啓用它們。不管怎樣,由於助記快捷方式不符閤 Aqua 指導方針,Qt 不會展示帶下劃綫的快捷方式字符。
對於使用菜單的應用程序,更方便可能是使用提供的方便函數在 QMenu 類以將鍵盤快捷方式賦值給菜單項 (當創建它們時)。另外,快捷方式可以關聯其它動作類型在 QAction 類。
為特定 Widget 創建快捷方式的最簡單辦法,是采用鍵序列構造快捷方式。例如:
shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), parent);
當用戶鍵入 鍵序列 為給定快捷方式,快捷方式的 activated () 信號被發射 (在歧義性情況下, activatedAmbiguously () 信號被發射)。由 Qt 事件循環 "監聽" 快捷方式,當快捷方式的父級 Widget 在接收事件時。
可以設置快捷方式的鍵序列采用 setKey () 和檢索采用 key ()。可以啓用 (或禁用) 快捷方式采用 setEnabled (),和可以擁有 What's This? 幫助文本設置采用 setWhatsThis ().
另請參閱 QShortcutEvent , QKeySequence ,和 QAction .
此特性保持快捷方式是否可以自動重復
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.
該特性在 Qt 4.2 引入。
訪問函數:
| bool | autoRepeat () const |
| void | setAutoRepeat (bool on ) |
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.
默認情況下,此特性被設為 Qt::WindowShortcut .
訪問函數:
| Qt::ShortcutContext | context () const |
| void | setContext (Qt::ShortcutContext context ) |
此特性保持快捷方式是否被啓用
An enabled shortcut emits the activated () 或 activatedAmbiguously () signal when a QShortcutEvent occurs that matches the shortcut's key () sequence.
若應用程序在
WhatsThis
mode the shortcut will not emit the signals, but will show the "What's This?" text instead.
默認情況下,此特性為
true
.
訪問函數:
| bool | isEnabled () const |
| void | setEnabled (bool enable ) |
另請參閱 whatsThis .
此特性保持快捷方式的鍵序列
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:
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
默認情況下,此特性包含空鍵序列。
訪問函數:
| QKeySequence | key () const |
| void | setKey (const QKeySequence & key ) |
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 (with or without a shortcut key), set the help on the item's action.
默認情況下,此特性包含空字符串。
訪問函數:
| QString | whatsThis () const |
| void | setWhatsThis (const QString & text ) |
另請參閱 QWhatsThis::inWhatsThisMode () 和 QAction::setWhatsThis ().
構造 QShortcut 對象為 parent 小部件。由於未指定快捷鍵序列,所以快捷方式不會發射任何信號。
另請參閱 setKey ().
構造 QShortcut 對象為 parent 小部件。快捷方式運轉於其父級,監聽 QShortcutEvent 匹配 key 序列。根據事件的歧義性,快捷方式將調用 member 函數,或 ambiguousMember 函數,若鍵按下在快捷方式的 context .
銷毀快捷方式。
[signal]
void
QShortcut::
activated
()
此信號被發射,當用戶鍵入快捷方式的鍵序列時。
另請參閱 activatedAmbiguously ().
[signal]
void
QShortcut::
activatedAmbiguously
()
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.
另請參閱 activated ().
返迴快捷方式 ID。
另請參閱 QShortcutEvent::shortcutId ().
返迴快捷方式的父級 Widget。