class QAction#

The QAction class provides an abstraction for user commands that can be added to different user interface components. More

Inheritance diagram of PySide6.QtGui.QAction

Inherited by: QWidgetAction

Synopsis#

Properties#

Methods#

Slots#

Signals#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

In applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. Since the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action.

Actions can be added to user interface elements such as menus and toolbars, and will automatically keep the UI in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked.

A QAction may contain an icon, descriptive text, icon text, a keyboard shortcut, status text, “What’s This?” text, and a tooltip. All properties can be set independently with setIcon() , setText() , setIconText() , setShortcut() , setStatusTip() , setWhatsThis() , and setToolTip() . Icon and text, as the two most important properties, can also be set in the constructor. It’s possible to set an individual font with setFont() , which e.g. menus respect when displaying the action as a menu item.

We recommend that actions are created as children of the window they are used in. In most cases actions will be children of the application’s main window.

QAction in widget applications#

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.

Actions are added to widgets using QWidget::addAction() or 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 class contains convenience functions for creating actions suitable for use as menu items.

See also

QMenuQToolBar

class MenuRole#

This enum describes how an action should be moved into the application menu on macOS.

Constant

Description

QAction.NoRole

This action should not be put into the application menu

QAction.TextHeuristicRole

This action should be put in the application menu based on the action’s text as described in the QMenuBar documentation.

QAction.ApplicationSpecificRole

This action should be put in the application menu with an application specific role

QAction.AboutQtRole

This action handles the “About Qt” menu item.

QAction.AboutRole

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

QAction.PreferencesRole

This action should be placed where the “Preferences…” menu item is in the application menu.

QAction.QuitRole

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.

class Priority#

This enum defines priorities for actions in user interface.

Constant

Description

QAction.LowPriority

The action should not be prioritized in the user interface.

QAction.NormalPriority

QAction.HighPriority

The action should be prioritized in the user interface.

See also

priority

New in version 4.6.

class ActionEvent#

This enum type is used when calling activate()

Constant

Description

QAction.Trigger

this will cause the triggered() signal to be emitted.

QAction.Hover

this will cause the hovered() signal to be emitted.

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property autoRepeatᅟ: bool#

This property holds whether the action can auto repeat.

If true, the action 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.

Access functions:
property checkableᅟ: bool#

This property holds whether the action is a checkable action.

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.

See also

setChecked()

Access functions:
property checkedᅟ: bool#

This property holds whether the action is checked..

Only checkable actions can be checked. By default, this is false (the action is unchecked).

Note

The notifier signal for this property is toggled() . As toggling a QAction changes its state, it will also emit a changed() signal.

See also

checkable toggled()

Access functions:
property enabledᅟ: bool#

This property holds whether the action is enabled.

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

By default, this property is true (actions are enabled).

See also

text

Access functions:
property fontᅟ: QFont#

This property holds the action’s font.

The font property is used to render the text set on the QAction . The font can be considered a hint as it will not be consulted in all cases based upon application and style.

By default, this property contains the application’s default font.

See also

setText()

Access functions:
property iconᅟ: QIcon#

This property holds the action’s icon.

In toolbars, the icon is used as the tool button icon; in menus, it is displayed to the left of the menu text. There is no default icon.

If a null icon ( isNull() ) is passed into this function, the icon of the action is cleared.

Access functions:
property iconTextᅟ: str#

This property holds the action’s descriptive icon text.

If 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() or setToolTip() , and will also be used in toolbar buttons if no icon has been defined using setIcon() .

If the icon text is not explicitly set, the action’s normal text will be used for the icon text.

By default, this property contains an empty string.

Access functions:
property iconVisibleInMenuᅟ: bool#

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

This property holds Whether or not an action should show an icon in a menu.

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 absence) of the attribute.

For example:

app = QApplication([])
app.setAttribute(Qt.AA_DontShowIconsInMenus) # Icons are no longer shown in menus
# ...
myAction = QAction()
# ...
myAction.setIcon(SomeIcon)
myAction.setIconVisibleInMenu(True) # Icon will be shown in menus for this action.

See also

icon setAttribute()

Access functions:
property menuRoleᅟ: QAction.MenuRole#

This property holds the action’s menu role.

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

Access functions:
property priorityᅟ: QAction.Priority#

This property holds the actions’s priority in the user interface..

This property can be set to indicate how the action should be prioritized in the user interface.

For instance, when toolbars have the Qt::ToolButtonTextBesideIcon mode set, then actions with LowPriority will not show the text labels.

Access functions:
property shortcutᅟ: QKeySequence#

This property holds the action’s primary shortcut key.

Valid keycodes for this property can be found in Qt::Key and Qt::Modifier. There is no default shortcut key.

Access functions:
property shortcutContextᅟ: Qt.ShortcutContext#

This property holds the context for the action’s shortcut.

Valid values for this property can be found in Qt::ShortcutContext. The default value is Qt::WindowShortcut.

Access functions:
property shortcutVisibleInContextMenuᅟ: bool#

This property holds Whether or not an action should show a shortcut in a context menu.

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. Explicitly setting this property overrides the attribute.

Access functions:
property statusTipᅟ: str#

This property holds the action’s status tip.

The status tip is displayed on all status bars provided by the action’s top-level parent widget.

By default, this property contains an empty string.

Access functions:
property textᅟ: str#

This property holds the action’s descriptive text.

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.

Certain UI elements, such as menus or buttons, can use ‘&’ in front of a character to automatically create a mnemonic (a shortcut) for that character. For example, “&File” for a menu will create the shortcut Alt+F, which will open the File menu. “E&xit” will create the shortcut Alt+X for a button, or in a menu allow navigating to the menu item by pressing “x”. (use ‘&&’ to display an actual ampersand). The widget might consume and perform an action on a given shortcut.

See also

iconText

Access functions:
property toolTipᅟ: str#

This property holds the action’s tooltip.

This text is used for the tooltip. If no tooltip is specified, the action’s text is used.

By default, this property contains the action’s text.

Access functions:
property visibleᅟ: bool#

This property holds whether the action can be seen (e.g. in menus and toolbars).

If 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.

By default, this property is true (actions are visible).

Access functions:
property whatsThisᅟ: str#

This property holds the action’s “What’s This?” help text.

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.

See also

QWhatsThis

Access functions:
__init__(icon, text[, parent=None])#
Parameters:

Constructs an action with an icon and some text and parent. If parent is an action group the action will be automatically inserted into the group.

A stripped version of text (for example, “&Menu Option…” becomes “Menu Option”) will be used for tooltips and icon text unless you specify a different text using setToolTip() or setIconText() , respectively.

See also

text icon

__init__([parent=None])
Parameters:

parentQObject

Constructs an action with parent. If parent is an action group the action will be automatically inserted into the group.

Note

The parent argument is optional since Qt 5.7.

__init__(text[, parent=None])
Parameters:

Constructs an action with some text and parent. If parent is an action group the action will be automatically inserted into the group.

A stripped version of text (for example, “&Menu Option…” becomes “Menu Option”) will be used for tooltips and icon text unless you specify a different text using setToolTip() or setIconText() , respectively.

See also

text

actionGroup()#
Return type:

QActionGroup

Returns the action group for this action. If no action group manages this action, then None will be returned.

activate(event)#
Parameters:

eventActionEvent

Sends the relevant signals for ActionEvent event.

Action-based widgets use this API to cause the QAction to emit signals as well as emitting their own.

associatedObjects()#
Return type:

.list of QObject

Returns a list of objects this action has been added to.

See also

addAction()

autoRepeat()#
Return type:

bool

See also

setAutoRepeat()

Getter of property autoRepeatᅟ .

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.

See also

actionEvent()

Notification signal of property iconᅟ .

checkableChanged(checkable)#
Parameters:

checkable – bool

Notification signal of property checkableᅟ .

data()#
Return type:

object

Returns the user data as set in setData .

See also

setData()

enabledChanged(enabled)#
Parameters:

enabled – bool

Notification signal of property enabledᅟ .

font()#
Return type:

QFont

See also

setFont()

Getter of property fontᅟ .

hover()#

This is a convenience slot that calls activate(Hover).

hovered()#

This signal is emitted when an action is highlighted by the user; for example, when the user pauses with the cursor over a menu option, toolbar button, or presses an action’s shortcut key combination.

See also

activate()

icon()#
Return type:

QIcon

See also

setIcon()

Getter of property iconᅟ .

iconText()#
Return type:

str

See also

setIconText()

Getter of property iconTextᅟ .

isCheckable()#
Return type:

bool

Getter of property checkableᅟ .

isChecked()#
Return type:

bool

Getter of property checkedᅟ .

isEnabled()#
Return type:

bool

Getter of property enabledᅟ .

isIconVisibleInMenu()#
Return type:

bool

Getter of property iconVisibleInMenuᅟ .

isSeparator()#
Return type:

bool

Returns true if this action is a separator action; otherwise it returns false.

See also

setSeparator()

isShortcutVisibleInContextMenu()#
Return type:

bool

Getter of property shortcutVisibleInContextMenuᅟ .

isVisible()#
Return type:

bool

Getter of property visibleᅟ .

menu()#
Return type:

QObject

menuRole()#
Return type:

MenuRole

See also

setMenuRole()

Getter of property menuRoleᅟ .

priority()#
Return type:

Priority

See also

setPriority()

Getter of property priorityᅟ .

resetEnabled()#

Reset function of property enabledᅟ .

setActionGroup(group)#
Parameters:

groupQActionGroup

Sets this action group to group. The action will be automatically added to the group’s list of actions.

Actions within the group will be mutually exclusive.

setAutoRepeat(arg__1)#
Parameters:

arg__1 – bool

See also

autoRepeat()

Setter of property autoRepeatᅟ .

setCheckable(arg__1)#
Parameters:

arg__1 – bool

See also

isCheckable()

Setter of property checkableᅟ .

setChecked(arg__1)#
Parameters:

arg__1 – bool

See also

isChecked()

Setter of property checkedᅟ .

setData(var)#
Parameters:

var – object

Sets the action’s internal data to the given data.

See also

data()

setDisabled(b)#
Parameters:

b – bool

This is a convenience function for the enabled property, that is useful for signals–slots connections. If b is true the action is disabled; otherwise it is enabled.

setEnabled(arg__1)#
Parameters:

arg__1 – bool

See also

isEnabled()

Setter of property enabledᅟ .

setFont(font)#
Parameters:

fontQFont

See also

font()

Setter of property fontᅟ .

setIcon(icon)#
Parameters:

iconQIcon

See also

icon()

Setter of property iconᅟ .

setIconText(text)#
Parameters:

text – str

See also

iconText()

Setter of property iconTextᅟ .

setIconVisibleInMenu(visible)#
Parameters:

visible – bool

Setter of property iconVisibleInMenuᅟ .

setMenu(arg__1)#
Parameters:

arg__1QObject

Sets the menu contained by this action to the specified menu.

See also

menu()

setMenuRole(menuRole)#
Parameters:

menuRoleMenuRole

See also

menuRole()

Setter of property menuRoleᅟ .

setPriority(priority)#
Parameters:

priorityPriority

See also

priority()

Setter of property priorityᅟ .

setSeparator(b)#
Parameters:

b – bool

If b is true then this action will be considered a separator.

How a separator is represented depends on the widget it is inserted into. Under most circumstances the text, submenu, and icon will be ignored for separator actions.

See also

isSeparator()

setShortcut(arg__1)#
Parameters:

arg__1Key

setShortcut(shortcut)
Parameters:

shortcutQKeySequence

Sets shortcut as the sole shortcut that triggers the action.

Setter of property shortcutᅟ .

setShortcutContext(context)#
Parameters:

contextShortcutContext

Setter of property shortcutContextᅟ .

setShortcutVisibleInContextMenu(show)#
Parameters:

show – bool

Setter of property shortcutVisibleInContextMenuᅟ .

setShortcuts(arg__1)#
Parameters:

arg__1StandardKey

Sets a platform dependent list of shortcuts based on the 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 instead.

See also

keyBindings()

setShortcuts(shortcuts)
Parameters:

shortcuts – .list of QKeySequence

Sets shortcuts as the list of shortcuts that trigger the action. The first element of the list is the primary shortcut.

setStatusTip(statusTip)#
Parameters:

statusTip – str

See also

statusTip()

Setter of property statusTipᅟ .

setText(text)#
Parameters:

text – str

See also

text()

Setter of property textᅟ .

setToolTip(tip)#
Parameters:

tip – str

See also

toolTip()

Setter of property toolTipᅟ .

setVisible(arg__1)#
Parameters:

arg__1 – bool

See also

isVisible()

Setter of property visibleᅟ .

setWhatsThis(what)#
Parameters:

what – str

See also

whatsThis()

Setter of property whatsThisᅟ .

shortcut()#
Return type:

QKeySequence

Returns the primary shortcut.

See also

setShortcuts()

Getter of property shortcutᅟ .

shortcutContext()#
Return type:

ShortcutContext

Getter of property shortcutContextᅟ .

shortcuts()#
Return type:

.list of QKeySequence

Returns the list of shortcuts, with the primary shortcut as the first element of the list.

See also

setShortcuts()

showStatusText([object=None])#
Parameters:

objectQObject

Return type:

bool

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

If a null widget is specified, the event is sent to the action’s parent.

See also

statusTip

statusTip()#
Return type:

str

See also

setStatusTip()

Getter of property statusTipᅟ .

text()#
Return type:

str

See also

setText()

Getter of property textᅟ .

toggle()#

This is a convenience function for the checked property. Connect to it to change the checked state to its opposite state.

toggled(arg__1)#
Parameters:

arg__1 – bool

This signal is emitted whenever a checkable action changes its isChecked() status. This can be the result of a user interaction, or because setChecked() was called. As setChecked() changes the QAction , it emits changed() in addition to toggled().

checked is true if the action is checked, or false if the action is unchecked.

See also

activate() triggered() checked

Notification signal of property checkedᅟ .

toolTip()#
Return type:

str

See also

setToolTip()

Getter of property toolTipᅟ .

trigger()#

This is a convenience slot that calls activate(Trigger).

triggered([checked=false])#
Parameters:

checked – bool

This signal is emitted when an action is activated by the user; for example, when the user clicks a menu option, toolbar button, or presses an action’s shortcut key combination, or when trigger() was called. Notably, it is not emitted when setChecked() or toggle() is called.

If the action is checkable, checked is true if the action is checked, or false if the action is unchecked.

See also

activate() toggled() checked

visibleChanged()#

Notification signal of property visibleᅟ .

whatsThis()#
Return type:

str

See also

setWhatsThis()

Getter of property whatsThisᅟ .