QShortcut 类

The QShortcut 类用于创建键盘快捷方式。 更多...

头: #include <QShortcut>
qmake: QT += widgets
继承: QObject

特性

公共函数

QShortcut (QWidget * parent )
QShortcut (const QKeySequence & key , QWidget * parent , const char * member = nullptr, const char * ambiguousMember = nullptr, Qt::ShortcutContext context = Qt::WindowShortcut)
virtual ~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 ()

静态公共成员

const QMetaObject staticMetaObject

额外继承成员

详细描述

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 .

特性文档编制

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.

该特性在 Qt 4.2 引入。

访问函数:

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 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 )

whatsThis : QString

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:: QShortcut ( QWidget * parent )

构造 QShortcut 对象为 parent 小部件。由于未指定快捷键序列,所以快捷方式不会发射任何信号。

另请参阅 setKey ().

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

构造 QShortcut 对象为 parent 小部件。快捷方式运转于其父级,监听 QShortcutEvent 匹配 key 序列。根据事件的歧义性,快捷方式将调用 member 函数,或 ambiguousMember 函数,若键按下在快捷方式的 context .

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

int QShortcut:: id () const

返回快捷方式 ID。

另请参阅 QShortcutEvent::shortcutId ().

QWidget *QShortcut:: parentWidget () const

返回快捷方式的父级 Widget。