QShortcut 類

QShortcut 類用於創建鍵盤快捷方式。 更多...

頭: #include <QShortcut>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
繼承: QObject

特性

公共函數

QShortcut (QObject * parent )
QShortcut (const QKeySequence & key , QObject * parent , const char * member = nullptr, const char * ambiguousMember = nullptr, Qt::ShortcutContext context = Qt::WindowShortcut)
QShortcut (QKeySequence::StandardKey standardKey , QObject * parent , const char * member = nullptr, const char * ambiguousMember = nullptr, Qt::ShortcutContext context = Qt::WindowShortcut)
QShortcut (const QKeySequence & key , QObject * parent , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
QShortcut (const QKeySequence & key , QObject * parent , const QObject * context , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
QShortcut (const QKeySequence & key , QObject * parent , const QObject * context , Functor functor , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
QShortcut (const QKeySequence & key , QObject * parent , const QObject * context1 , Functor functor , const QObject * context2 , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
QShortcut (QKeySequence::StandardKey key , QObject * parent , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
QShortcut (QKeySequence::StandardKey key , QObject * parent , const QObject * context , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
QShortcut (QKeySequence::StandardKey key , QObject * parent , const QObject * context , Functor functor , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
QShortcut (QKeySequence::StandardKey key , QObject * parent , const QObject * context1 , Functor functor , const QObject * context2 , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)
virtual ~QShortcut ()
bool autoRepeat () const
Qt::ShortcutContext context () const
bool isEnabled () const
QKeySequence key () const
QList<QKeySequence> keys () const
QWidget * parentWidget () const
void setAutoRepeat (bool on )
void setContext (Qt::ShortcutContext context )
void setEnabled (bool enable )
void setKey (const QKeySequence & key )
void setKeys (QKeySequence::StandardKey key )
void setKeys (const QList<QKeySequence> & keys )
void setWhatsThis (const QString & text )
QString whatsThis () const

信號

void activated ()
void activatedAmbiguously ()

詳細描述

QShortcut 類提供辦法以將鍵盤快捷方式連接到 Qt 的 信號/槽 機製,以便可以通報對象當執行快捷方式時。可以將快捷鍵方式設為包含描述鍵盤快捷方式的所有必要鍵按下,包括修飾符鍵的狀態,譬如 Shift , Ctrl ,和 Alt .

In widget applications, certain widgets can use '&' in front of a character. This 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 .

特性文檔編製

autoRepeat : bool

此特性保持快捷方式是否可以自動重復

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.

訪問函數:

bool autoRepeat () const
void setAutoRepeat (bool on )

context : Qt::ShortcutContext

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 )

enabled : bool

此特性保持快捷方式是否被啓用

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 .

key : QKeySequence

This property holds the shortcut's primary key sequence

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 )

成員函數文檔編製

[explicit] QShortcut:: QShortcut ( QObject * parent )

Constructs a QShortcut object for the parent , which should be a QWindow QWidget .

Since no shortcut key sequence is specified, the shortcut will not emit any signals.

另請參閱 setKey ().

[explicit] QShortcut:: QShortcut (const QKeySequence & key , QObject * parent , const char * member = nullptr, const char * ambiguousMember = nullptr, Qt::ShortcutContext context = Qt::WindowShortcut)

Constructs a QShortcut object for the parent , which should be a QWindow QWidget .

The shortcut operates on its parent, listening for QShortcutEvent 匹配 key 序列。根據事件的歧義性,快捷方式將調用 member 函數,或 ambiguousMember 函數,若鍵按下在快捷方式的 context .

[explicit, since 6.0] QShortcut:: QShortcut ( QKeySequence::StandardKey standardKey , QObject * parent , const char * member = nullptr, const char * ambiguousMember = nullptr, Qt::ShortcutContext context = Qt::WindowShortcut)

Constructs a QShortcut object for the parent , which should be a QWindow QWidget .

The shortcut operates on its parent, listening for QShortcutEvent 匹配 standardKey . Depending on the ambiguity of the event, the shortcut will call the member 函數,或 ambiguousMember 函數,若鍵按下在快捷方式的 context .

該函數在 Qt 6.0 引入。

template <typename Functor> QShortcut:: QShortcut (const QKeySequence & key , QObject * parent , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor .

template <typename Functor> QShortcut:: QShortcut (const QKeySequence & key , QObject * parent , const QObject * context , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor .

The functor can be a pointer to a member function of the context 對象。

context object is destroyed, the functor 不會被調用。

template <typename Functor, typename FunctorAmbiguous> QShortcut:: QShortcut (const QKeySequence & key , QObject * parent , const QObject * context , Functor functor , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor and activatedAmbiguously () 信號到 functorAmbiguous .

The functor and functorAmbiguous can be a pointer to a member function of the context 對象。

context object is destroyed, the functor and functorAmbiguous 不會被調用。

template <typename Functor, typename FunctorAmbiguous> QShortcut:: QShortcut (const QKeySequence & key , QObject * parent , const QObject * context1 , Functor functor , const QObject * context2 , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor and activatedAmbiguously () 信號到 functorAmbiguous .

The functor can be a pointer to a member function of the context1 對象。 functorAmbiguous can be a pointer to a member function of the context2 對象。

context1 object is destroyed, the functor will not be called. If the context2 object is destroyed, the functorAmbiguous 不會被調用。

[since 6.0] template <typename Functor> QShortcut:: QShortcut ( QKeySequence::StandardKey key , QObject * parent , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor .

該函數在 Qt 6.0 引入。

[since 6.0] template <typename Functor> QShortcut:: QShortcut ( QKeySequence::StandardKey key , QObject * parent , const QObject * context , Functor functor , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor .

The functor can be a pointer to a member function of the context 對象。

context object is destroyed, the functor 不會被調用。

該函數在 Qt 6.0 引入。

[since 6.0] template <typename Functor, typename FunctorAmbiguous> QShortcut:: QShortcut ( QKeySequence::StandardKey key , QObject * parent , const QObject * context , Functor functor , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor and activatedAmbiguously () 信號到 functorAmbiguous .

The functor and functorAmbiguous can be a pointer to a member function of the context 對象。

context object is destroyed, the functor and functorAmbiguous 不會被調用。

該函數在 Qt 6.0 引入。

[since 6.0] template <typename Functor, typename FunctorAmbiguous> QShortcut:: QShortcut ( QKeySequence::StandardKey key , QObject * parent , const QObject * context1 , Functor functor , const QObject * context2 , FunctorAmbiguous functorAmbiguous , Qt::ShortcutContext shortcutContext = Qt::WindowShortcut)

這是重載函數。

This is a QShortcut convenience constructor which connects the shortcut's activated () 信號到 functor and activatedAmbiguously () 信號到 functorAmbiguous .

The functor can be a pointer to a member function of the context1 對象。 functorAmbiguous can be a pointer to a member function of the context2 對象。

context1 object is destroyed, the functor will not be called. If the context2 object is destroyed, the functorAmbiguous 不會被調用。

該函數在 Qt 6.0 引入。

[虛擬] QShortcut:: ~QShortcut ()

銷毀快捷方式。

[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 ().

[since 6.0] QList < QKeySequence > QShortcut:: keys () const

Returns the list of key sequences which trigger this shortcut.

該函數在 Qt 6.0 引入。

另請參閱 key and setKeys ().

QWidget *QShortcut:: parentWidget () const

返迴快捷方式的父級 Widget。

[since 6.0] void QShortcut:: setKeys ( QKeySequence::StandardKey key )

Sets the triggers to those matching the standard key key .

該函數在 Qt 6.0 引入。

另請參閱 key and keys ().

[since 6.0] void QShortcut:: setKeys (const QList < QKeySequence > & keys )

設置 keys as the list of key sequences that trigger the shortcut.

該函數在 Qt 6.0 引入。

另請參閱 key and keys ().

void QShortcut:: setWhatsThis (const QString & text )

Sets the shortcut's "What's This?" help text .

The text will be shown when a widget 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.

By default, the help text is an empty string.

This function has no effect in applications that don't use widgets.

另請參閱 whatsThis (), QWhatsThis::inWhatsThisMode (),和 QAction::setWhatsThis ().

QString QShortcut:: whatsThis () const

Returns the shortcut's "What's This?" help text.

另請參閱 setWhatsThis ().