Action QML Type

Abstract user interface action. More...

Import Statement: import QtQuick.Controls 2.2
Since: Qt 5.10
Inherits:

QtObject

Properties

Signals

Methods

Detailed Description

Action represents an abstract user interface action that can have shortcuts and can be assigned to menu items and toolbar buttons.

Actions may contain text, an icon, and a shortcut. Actions are normally triggered by the user via menu items, toolbar buttons, or keyboard shortcuts. A checkable Action toggles its checked state when triggered.

 Action {
     id: copyAction
     text: qsTr("&Copy")
     icon.name: "edit-copy"
     shortcut: StandardKey.Copy
     onTriggered: window.activeFocusItem.copy()
 }

Action is commonly used to implement application commands that can be invoked via menu items, toolbar buttons, and keyboard shortcuts. Since the user expects the commands to be performed in the same way, regardless of the user interface used, it is useful to represent the commands as shareable actions.

Action can be also used to separate the logic and the visual presentation. For example, when declaring buttons and menu items in .ui.qml files, actions can be declared elsewhere and assigned from the outside.

 ToolButton {
     id: toolButton
     action: copyAction
 }

When an action is paired with buttons and menu items, the enabled, checkable, and checked states are synced automatically. For example, in a word processor, if the user clicks a "Bold" toolbar button, the "Bold" menu item will automatically be checked. Buttons and menu items get their text and icon from the action by default. An action-specific text or icon can be overridden for a specific control by specifying text or icon directly on the control.

 MenuItem {
     id: menuItem
     action: copyAction
     text: qsTr("&Copy selected Text")
 }

Since Action presents a user interface action, it is intended to be assigned to a MenuItem, ToolButton, or any other control that inherits AbstractButton. For keyboard shortcuts, the simpler Shortcut type is more appropriate.

See also MenuItem, ToolButton, and Shortcut.

Property Documentation

checkable : bool

This property holds whether the action is checkable. The default value is false.

A checkable action toggles between checked (on) and unchecked (off) when triggered.

See also checked.


checked : bool

This property holds whether the action is checked.

See also checkable.


enabled : bool

This property holds whether the action is enabled. The default value is true.


icon group

icon.cache : bool

icon.color : color

icon.height : int

icon.name : string

icon.source : url

icon.width : int

NameDescription
nameThis property holds the name of the icon to use.

The icon will be loaded from the platform theme. If the icon is found in the theme, it will always be used; even if icon.source is also set. If the icon is not found, icon.source will be used instead.

For more information on theme icons, see QIcon::fromTheme().

sourceThis property holds the name of the icon to use.

The icon will be loaded as a regular image.

If icon.name is set and refers to a valid theme icon, it will always be used instead of this property.

widthThis property holds the width of the icon.

The icon's width will never exceed this value, though it will shrink when necessary.

heightThis property holds the height of the icon.

The icon's height will never exceed this value, though it will shrink when necessary.

colorThis property holds the color of the icon.

The icon is tinted with the specified color, unless the color is set to "transparent".

cacheThis property specifies whether the icon should be cached.

The default value is true.

For more information, see cache.

This property was introduced in QtQuick.Controls 2.13.


shortcut : keysequence

This property holds the action's shortcut. The key sequence can be set to one of the standard keyboard shortcuts, or it can be described with a string containing a sequence of up to four key presses that are needed to trigger the shortcut.

 Action {
     shortcut: "Ctrl+E,Ctrl+W"
     onTriggered: edit.wrapMode = TextEdit.Wrap
 }

text : string

This property holds a textual description of the action.


Signal Documentation

toggled(QtObject source)

This signal is emitted when the action is toggled. The source argument identifies the object that toggled the action.

For example, if the action is assigned to a menu item and a toolbar button, the action is toggled when the control is toggled, the shortcut is activated, or when toggle() is called directly.

Note: The corresponding handler is onToggled.


triggered(QtObject source)

This signal is emitted when the action is triggered. The source argument identifies the object that triggered the action.

For example, if the action is assigned to a menu item and a toolbar button, the action is triggered when the control is clicked, the shortcut is activated, or when trigger() is called directly.

Note: The corresponding handler is onTriggered.


Method Documentation

void toggle(QtObject source)

Toggles the action and emits toggled() if enabled, with an optional source object defined.


void trigger(QtObject source)

Triggers the action and emits triggered() if enabled, with an optional source object defined.