QAction 类

QAction 类提供可以被添加到不同用户界面组件的用户命令的抽象。 更多...

Header: #include <QAction>
CMake: find_package(Qt6 COMPONENTS Gui REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
Since: Qt 6.0
继承: QObject
继承者:

QWidgetAction

公共类型

enum ActionEvent { Trigger, Hover }
enum MenuRole { NoRole, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole, AboutRole, …, QuitRole }
enum Priority { LowPriority, NormalPriority, HighPriority }

特性

公共函数

QAction (const QIcon & icon , const QString & text , QObject * parent = nullptr)
QAction (const QString & text , QObject * parent = nullptr)
QAction (QObject * parent = nullptr)
virtual ~QAction ()
QActionGroup * actionGroup () const
void activate (QAction::ActionEvent event )
QList<QObject *> associatedObjects () const
bool autoRepeat () const
QVariant data () const
QFont font () const
QIcon icon () const
QString iconText () const
bool isCheckable () const
bool isChecked () const
bool isEnabled () const
bool isIconVisibleInMenu () const
bool isSeparator () const
bool isShortcutVisibleInContextMenu () const
bool isVisible () const
QAction::MenuRole menuRole () const
QAction::Priority priority () const
void setActionGroup (QActionGroup * group )
void setAutoRepeat (bool)
void setCheckable (bool)
void setData (const QVariant & data )
void setFont (const QFont & font )
void setIcon (const QIcon & icon )
void setIconText (const QString & text )
void setIconVisibleInMenu (bool visible )
void setMenuRole (QAction::MenuRole menuRole )
void setPriority (QAction::Priority priority )
void setSeparator (bool b )
void setShortcut (const QKeySequence & shortcut )
void setShortcutContext (Qt::ShortcutContext context )
void setShortcutVisibleInContextMenu (bool show )
void setShortcuts (const QList<QKeySequence> & shortcuts )
void setShortcuts (QKeySequence::StandardKey key )
void setStatusTip (const QString & statusTip )
void setText (const QString & text )
void setToolTip (const QString & tip )
void setWhatsThis (const QString & what )
QKeySequence shortcut () const
Qt::ShortcutContext shortcutContext () const
QList<QKeySequence> shortcuts () const
bool showStatusText (QObject * object = nullptr)
QString statusTip () const
QString text () const
QString toolTip () const
QString whatsThis () const

公共槽

void hover ()
void resetEnabled ()
void setChecked (bool)
void setDisabled (bool b )
void setEnabled (bool)
void setVisible (bool)
void toggle ()
void trigger ()

信号

void changed ()
void checkableChanged (bool checkable )
void enabledChanged (bool enabled )
void hovered ()
void toggled (bool checked )
void triggered (bool checked = false)
void visibleChanged ()

重实现保护函数

virtual bool event (QEvent * e ) override

详细描述

在应用程序中,许多常见命令可以凭借菜单、工具栏按钮及键盘快捷键援引。由于用户期望每个命令以相同方式履行,不管所用用户界面,它是有用的将每个命令表示成 action .

动作可以被添加到菜单和工具栏,并自动保持同步。例如,在单词处理程序中,若用户按下加粗工具栏按钮,加粗菜单项将被自动选中。

QAction 可能包含图标、菜单文本、快捷方式、状态文本、What's This? 文本及工具提示。 这些中的大多数可以在构造函数中设置。也可独立设置它们采用 setIcon (), setText (), setIconText (), setShortcut (), setStatusTip (), setWhatsThis (),和 setToolTip ()。对于菜单项,可以设置单个字体采用 setFont ().

推荐将动作创建作为在其中使用它们的窗口子级。在大多数情况下,动作将是应用程序主窗口的子级。

在 Widget 应用程序中的 QAction

Once a QAction has been created, it should be added to the relevant menu and toolbar, then connected to the slot which will perform the action. For example:

    const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
    QAction *openAct = new QAction(openIcon, tr("&Open..."), this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setStatusTip(tr("Open an existing file"));
    connect(openAct, &QAction::triggered, this, &MainWindow::open);
    fileMenu->addAction(openAct);
    fileToolBar->addAction(openAct);
							

动作被添加到 Widget 使用 QWidget::addAction () 或 QGraphicsWidget::addAction (). Note that an action must be added to a widget before it can be used. This is also true when the shortcut should be global (i.e., Qt::ApplicationShortcut as Qt::ShortcutContext ).

Actions can be created as independent objects. But they may also be created during the construction of menus. The QMenu 类包含用于创建适合用作菜单项的动作的方便函数。

另请参阅 QMenu , QToolBar ,和 Qt Widgets - 应用程序范例 .

成员类型文档编制

enum QAction:: ActionEvent

使用此枚举类型当调用 QAction::activate ()

常量 描述
QAction::Trigger 0 这将导致 QAction::triggered () 信号被发射。
QAction::Hover 1 这将导致 QAction::hovered () 信号被发射。

此枚举描述应该如何把动作移入 macOS 应用程序菜单。

常量 描述
QAction::NoRole 0 此动作不应放入应用程序菜单中
QAction::TextHeuristicRole 1 This action should be put in the application menu based on the action's text as described in the QMenuBar 文档编制。
QAction::ApplicationSpecificRole 2 This action should be put in the application menu with an application specific role
QAction::AboutQtRole 3 此动作处理 About Qt 菜单项。
QAction::AboutRole 4 This action should be placed where the "About" menu item is in the application menu. The text of the menu item will be set to "About <application name>". The application name is fetched from the Info.plist file in the application's bundle (See Qt for macOS - 部署 ).
QAction::PreferencesRole 5 This action should be placed where the "Preferences..." menu item is in the application menu.
QAction::QuitRole 6 This action should be placed where the Quit menu item is in the application menu.

Setting this value only has effect on items that are in the immediate menus of the menubar, not the submenus of those menus. For example, if you have File menu in your menubar and the File menu has a submenu, setting the MenuRole for the actions in that submenu have no effect. They will never be moved.

enum QAction:: Priority

此枚举定义用户界面中动作的优先级。

常量 描述
QAction::LowPriority 0 动作不应该在用户界面中被优先。
QAction::NormalPriority 128
QAction::HighPriority 256 动作应该在用户界面中被优先。

另请参阅 priority .

特性文档编制

autoRepeat : bool

此特性保持动作是否可以自动重复

若为 true,动作会自动重复当按下键盘快捷方式组合时,前提是系统启用了键盘自动重复。默认值为 true。

访问函数:

bool autoRepeat () const
void setAutoRepeat (bool)

通知程序信号:

void changed ()

checkable : bool

此特性保持动作是否为可复选动作

A checkable action is one which has an on/off state. For example, in a word processor, a Bold toolbar button may be either on or off. An action which is not a toggle action is a command action; a command action is simply executed, e.g. file save. By default, this property is false .

In some situations, the state of one toggle action should depend on the state of others. For example, "Left Align", "Center" and "Right Align" toggle actions are mutually exclusive. To achieve exclusive toggling, add the relevant toggle actions to a QActionGroup with the QActionGroup::exclusive property set to true.

访问函数:

bool isCheckable () const
void setCheckable (bool)

通知程序信号:

void checkableChanged (bool checkable )

另请参阅 setChecked ().

checked : bool

此特性保持动作是否被复选。

仅可复选动作可以被复选。默认情况下,这为 false (动作未被复选)。

注意: 此特性的通知信号为 toggled ()。作为触发 QAction 改变其状态,它还会发射 changed () 信号。

访问函数:

bool isChecked () const
void setChecked (bool)

通知程序信号:

void toggled (bool checked )

另请参阅 checkable and toggled ().

enabled : bool

此特性保持动作是否被启用

Disabled actions cannot be chosen by the user. They do not disappear from menus or toolbars, but they are displayed in a way which indicates that they are unavailable. For example, they might be displayed using only shades of gray.

What's This? help on disabled actions is still available, provided that the QAction::whatsThis property is set.

An action will be disabled when all widgets to which it is added (with QWidget::addAction ()) are disabled or not visible. When an action is disabled, it is not possible to trigger it through its shortcut.

默认情况下此特性为 true (动作被启用)。

访问函数:

bool isEnabled () const
void setEnabled (bool)
void resetEnabled ()

通知程序信号:

void enabledChanged (bool enabled )

另请参阅 text .

font : QFont

此特性保持动作的字体

字体特性用于渲染文本设置在 QAction . The font can be considered a hint as it will not be consulted in all cases based upon application and style.

默认情况下,此特性包含应用程序默认字体。

访问函数:

QFont font () const
void setFont (const QFont & font )

通知程序信号:

void changed ()

另请参阅 setText ().

icon : QIcon

此特性保持动作的图标

在工具栏中,图标被用作工具按钮图标;在菜单中,它显示在菜单文本左侧。不存在默认图标。

若 null 图标 ( QIcon::isNull ()) 被传入此函数,动作图标被清零。

访问函数:

QIcon icon () const
void setIcon (const QIcon & icon )

通知程序信号:

void changed ()

iconText : QString

此特性保持动作的描述性图标文本

QToolBar::toolButtonStyle is set to a value that permits text to be displayed, the text defined held in this property appears as a label in the relevant tool button.

It also serves as the default text in menus and tooltips if the action has not been defined with setText () 或 setToolTip (), and will also be used in toolbar buttons if no icon has been defined using setIcon ().

若未明确设置图标文本,动作的正常文本将用于图标文本。

默认情况下,此特性包含空字符串。

访问函数:

QString iconText () const
void setIconText (const QString & text )

通知程序信号:

void changed ()

另请参阅 setToolTip () 和 setStatusTip ().

iconVisibleInMenu : bool

此特性保持动作是否应在菜单中展示图标

In some applications, it may make sense to have actions with icons in the toolbar, but not in menus. If true, the icon (if valid) is shown in the menu, when it is false, it is not shown.

The default is to follow whether the Qt::AA_DontShowIconsInMenus attribute is set for the application. Explicitly settings this property overrides the presence (or abscence) of the attribute.

例如:

QApplication app(argc, argv);
app.setAttribute(Qt::AA_DontShowIconsInMenus);  // Icons are *no longer shown* in menus
// ...
QAction *myAction = new QAction();
// ...
myAction->setIcon(SomeIcon);
myAction->setIconVisibleInMenu(true);   // Icon *will* be shown in menus for *this* action.
							

访问函数:

bool isIconVisibleInMenu () const
void setIconVisibleInMenu (bool visible )

通知程序信号:

void changed ()

另请参阅 icon and QCoreApplication::setAttribute ().

此特性保持动作的菜单角色

This indicates what role the action serves in the application menu on macOS. By default all actions have the TextHeuristicRole , which means that the action is added based on its text (see QMenuBar for more information).

The menu role can only be changed before the actions are put into the menu bar in macOS (usually just before the first application window is shown).

访问函数:

QAction::MenuRole menuRole () const
void setMenuRole (QAction::MenuRole menuRole )

通知程序信号:

void changed ()

priority : Priority

此特性保持在用户界面中动作的优先级。

可以设置此特性以指示动作在用户界面中的优先级应该如何。

对于实例,当工具栏拥有 Qt::ToolButtonTextBesideIcon 模式设置,那么动作具有 LowPriority 被不展示文本标签。

访问函数:

QAction::Priority priority () const
void setPriority (QAction::Priority priority )

shortcut : QKeySequence

此特性保持动作的首要快捷键

可以找到此特性的有效键码在 Qt::Key and Qt::Modifier 。没有默认的快捷键。

访问函数:

QKeySequence shortcut () const
void setShortcut (const QKeySequence & shortcut )

通知程序信号:

void changed ()

shortcutContext : Qt::ShortcutContext

此特性保持动作快捷方式的上下文

可以找到此特性的有效值在 Qt::ShortcutContext 。默认值为 Qt::WindowShortcut .

访问函数:

Qt::ShortcutContext shortcutContext () const
void setShortcutContext (Qt::ShortcutContext context )

通知程序信号:

void changed ()

shortcutVisibleInContextMenu : bool

此特性保持动作是否应该在上下文菜单中展示快捷方式

In some applications, it may make sense to have actions with shortcuts in context menus. If true, the shortcut (if valid) is shown when the action is shown via a context menu, when it is false, it is not shown.

The default is to follow whether the Qt::AA_DontShowShortcutsInContextMenus attribute is set for the application, falling back to the widget style hint. Explicitly setting this property overrides the presence (or abscence) of the attribute.

访问函数:

bool isShortcutVisibleInContextMenu () const
void setShortcutVisibleInContextMenu (bool show )

通知程序信号:

void changed ()

另请参阅 shortcut and QCoreApplication::setAttribute ().

statusTip : QString

此特性保持动作的状态提示

状态提示显示在由动作的顶层父级 Widget 提供的所有状态栏上。

默认情况下,此特性包含空字符串。

访问函数:

QString statusTip () const
void setStatusTip (const QString & statusTip )

通知程序信号:

void changed ()

另请参阅 setToolTip () 和 showStatusText ().

text : QString

此特性保持动作的描述性文本

If the action is added to a menu, the menu option will consist of the icon (if there is one), the text, and the shortcut (if there is one). If the text is not explicitly set in the constructor, or by using setText(), the action's description icon text will be used as text. There is no default text.

访问函数:

QString text () const
void setText (const QString & text )

通知程序信号:

void changed ()

另请参阅 iconText .

toolTip : QString

此特性保持动作的工具提示

此文本用于工具提示。若工具提示未指定,使用动作文本。

默认情况下,此特性包含动作的文本。

访问函数:

QString toolTip () const
void setToolTip (const QString & tip )

通知程序信号:

void changed ()

另请参阅 setStatusTip () 和 setShortcut ().

visible : bool

此特性保持动作是否可见 (如在菜单和工具栏中)

visible is true the action can be seen (e.g. in menus and toolbars) and chosen by the user; if visible is false the action cannot be seen or chosen by the user.

Actions which are not visible are not grayed out; they do not appear at all.

默认情况下此特性为 true (动作可见)。

访问函数:

bool isVisible () const
void setVisible (bool)

通知程序信号:

void visibleChanged ()

whatsThis : QString

此特性保持动作的 What's This? 帮助文本

The "What's This?" text is used to provide a brief description of the action. The text may contain rich text. There is no default "What's This?" text.

访问函数:

QString whatsThis () const
void setWhatsThis (const QString & what )

通知程序信号:

void changed ()

另请参阅 QWhatsThis .

成员函数文档编制

QAction:: QAction (const QIcon & icon , const QString & text , QObject * parent = nullptr)

构造动作采用 icon 和一些 text and parent 。若 parent 是动作组,动作会被自动插入组中。

The action uses a stripped version of text (e.g. "&Menu Option..." becomes "Menu Option") as descriptive text for tool buttons. You can override this by setting a specific description with setText (). The same text will be used for tooltips unless you specify a different text using setToolTip ().

QAction:: QAction (const QString & text , QObject * parent = nullptr)

构造动作采用一些 text and parent 。若 parent 是动作组,动作会被自动插入组中。

The action uses a stripped version of text (e.g. "&Menu Option..." becomes "Menu Option") as descriptive text for tool buttons. You can override this by setting a specific description with setText (). The same text will be used for tooltips unless you specify a different text using setToolTip ().

QAction:: QAction ( QObject * parent = nullptr)

构造动作采用 parent 。若 parent 是动作组,动作会被自动插入组中。

注意: parent 自变量是可选的,从 Qt 5.7 起。

[signal] void QAction:: changed ()

This signal is emitted when an action has changed. If you are only interested in actions in a given widget, you can watch for QWidget::actionEvent () sent with an QEvent::ActionChanged .

注意: 通知程序信号为特性 autoRepeat . Notifier signal for property font . Notifier signal for property icon . Notifier signal for property iconText . Notifier signal for property iconVisibleInMenu . Notifier signal for property menuRole . Notifier signal for property shortcut . Notifier signal for property shortcutContext . Notifier signal for property shortcutVisibleInContextMenu . Notifier signal for property statusTip . Notifier signal for property text . Notifier signal for property toolTip . Notifier signal for property whatsThis .

另请参阅 QWidget::actionEvent ().

[slot] void QAction:: hover ()

这是调用 activate(Hover) 的方便槽。

[signal] void QAction:: hovered ()

此信号被发射当用户突显动作时;例如:当用户暂停光标在菜单选项、工具栏按钮上或按下动作的快捷键组合时。

另请参阅 activate ().

[slot] void QAction:: setDisabled ( bool b )

这是方便函数对于 enabled property, that is useful for signals--slots connections. If b is true the action is disabled; otherwise it is enabled.

[slot] void QAction:: toggle ()

这是方便函数对于 checked property. Connect to it to change the checked state to its opposite state.

[signal] void QAction:: toggled ( bool checked )

此信号被发射每当可复选动作改变其 isChecked () 状态。这可能是用户交互结果,或因为 setChecked () 被调用。因为 setChecked () 改变 QAction ,它发射 changed () 除 toggled() 外。

checked 为 true 若动作被复选,或 false 若动作被取消复选。

注意: 通知程序信号为特性 checked .

另请参阅 activate (), triggered (),和 checked .

[slot] void QAction:: trigger ()

这是调用 activate(Trigger) 的方便槽。

[signal] void QAction:: triggered ( bool checked = false)

此信号被发射当动作被激活由用户;例如:当用户点击菜单选项、工具栏按钮或按下动作的快捷键组合时,或当 trigger () 被调用。显而易见,它是 not 被发射当 setChecked () 或 toggle () 被调用。

若动作是可复选的, checked 为 true 若动作被复选,或 false 若动作被取消复选。

另请参阅 activate (), toggled (),和 checked .

[virtual] QAction:: ~QAction ()

销毁对象并释放分配的资源。

QActionGroup *QAction:: actionGroup () const

Returns the action group for this action. If no action group manages this action, then nullptr 将被返回。

另请参阅 QActionGroup and setActionGroup ().

void QAction:: activate ( QAction::ActionEvent event )

发送相关信号为 ActionEvent event .

Action-based widgets use this API to cause the QAction 去发射信号及发射它们自己。

[since 6.0] QList < QObject *> QAction:: associatedObjects () const

返回此动作已添加到的对象列表。

该函数在 Qt 6.0 引入。

另请参阅 QWidget::addAction () 和 QGraphicsWidget::addAction ().

QVariant QAction:: data () const

返回用户设置数据在 QAction::setData .

另请参阅 setData ().

[override virtual protected] bool QAction:: event ( QEvent * e )

重实现: QObject::event (QEvent *e).

bool QAction:: isSeparator () const

返回 true 若此动作是分隔符动作;否则它返回 false .

另请参阅 setSeparator ().

void QAction:: setActionGroup ( QActionGroup * group )

把此动作组设为 group 。动作会被自动添加到组的动作列表。

组内动作相互排斥。

另请参阅 QActionGroup and actionGroup ().

void QAction:: setData (const QVariant & data )

把动作的内部数据设为给定 data .

另请参阅 data ().

void QAction:: setSeparator ( bool b )

b 为 true,则此动作将被认为是分隔符。

如何表示分隔符从属插入其的 Widget。在大多数情况下,文本、子菜单及图标会被分隔符动作忽略。

另请参阅 isSeparator ().

void QAction:: setShortcut (const QKeySequence & shortcut )

设置 shortcut 作为触发动作的独家快捷方式。

注意: Setter 函数为特性 shortcut .

另请参阅 shortcut and setShortcuts ().

void QAction:: setShortcuts (const QList < QKeySequence > & shortcuts )

设置 shortcuts 作为触发动作的快捷方式列表。列表的第一元素是首要快捷方式。

另请参阅 shortcuts (), shortcut ,和 setShortcut ().

void QAction:: setShortcuts ( QKeySequence::StandardKey key )

设置从属平台的快捷方式列表基于 key . The result of calling this function will depend on the currently running platform. Note that more than one shortcut can assigned by this action. If only the primary shortcut is required, use setShortcut 代替。

另请参阅 QKeySequence::keyBindings ().

QKeySequence QAction:: shortcut () const

返回首要快捷键。

注意: Getter 函数对于特性 shortcut。

另请参阅 setShortcuts ().

QList < QKeySequence > QAction:: shortcuts () const

返回快捷键列表,采用首要快捷键作为列表的第一元素。

另请参阅 setShortcuts ().

bool QAction:: showStatusText ( QObject * object = nullptr)

Updates the relevant status bar for the UI represented by object by sending a QStatusTipEvent 。返回 true if an event was sent, otherwise returns false .

若指定 null 小部件,则事件被发送给动作的父级。

另请参阅 statusTip .