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 .

在 Widget 应用程序中,某些 Widget 可以在字符前使用 &。这会为该字符自动创建助记符 (快捷方式),如 E&xit 将创建快捷方式 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 ().