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

Functions

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 menus and toolbars, and will automatically keep them 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, menu text, a shortcut, status text, “What’s This?” text, and a tooltip. Most of these can be set in the constructor. They can also be set independently with setIcon() , setText() , setIconText() , setShortcut() , setStatusTip() , setWhatsThis() , and setToolTip() . For menu items, it is possible to set an individual font with setFont() .

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

Actions are added to widgets using addAction() or 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., ApplicationShortcut as 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

QMenu QToolBar Qt Widgets - Application Example

class PySide6.QtGui.QAction([parent=None])

PySide6.QtGui.QAction(icon, text[, parent=None])

PySide6.QtGui.QAction(text[, parent=None])

Parameters

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.

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.

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

PySide6.QtGui.QAction.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 for the actions in that submenu have no effect. They will never be moved.

PySide6.QtGui.QAction.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.

PySide6.QtGui.QAction.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.

PySide6.QtGui.QAction.actionGroup()
Return type

PySide6.QtGui.QActionGroup

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

PySide6.QtGui.QAction.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.

PySide6.QtGui.QAction.associatedObjects()
Return type

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

See also

addAction() addAction()

PySide6.QtGui.QAction.autoRepeat()
Return type

bool

See also

setAutoRepeat()

PySide6.QtGui.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 sent with an ActionChanged .

See also

actionEvent()

PySide6.QtGui.QAction.checkableChanged(checkable)
Parameters

checkable – bool

PySide6.QtGui.QAction.data()
Return type

object

Returns the user data as set in setData .

See also

setData()

PySide6.QtGui.QAction.enabledChanged(enabled)
Parameters

enabled – bool

PySide6.QtGui.QAction.font()
Return type

PySide6.QtGui.QFont

See also

setFont()

PySide6.QtGui.QAction.hover()

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

PySide6.QtGui.QAction.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()

PySide6.QtGui.QAction.icon()
Return type

PySide6.QtGui.QIcon

See also

setIcon()

PySide6.QtGui.QAction.iconText()
Return type

str

See also

setIconText()

PySide6.QtGui.QAction.isCheckable()
Return type

bool

PySide6.QtGui.QAction.isChecked()
Return type

bool

PySide6.QtGui.QAction.isEnabled()
Return type

bool

PySide6.QtGui.QAction.isIconVisibleInMenu()
Return type

bool

PySide6.QtGui.QAction.isSeparator()
Return type

bool

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

See also

setSeparator()

PySide6.QtGui.QAction.isShortcutVisibleInContextMenu()
Return type

bool

PySide6.QtGui.QAction.isVisible()
Return type

bool

PySide6.QtGui.QAction.menuRole()
Return type

MenuRole

See also

setMenuRole()

PySide6.QtGui.QAction.priority()
Return type

Priority

See also

setPriority()

PySide6.QtGui.QAction.resetEnabled()
PySide6.QtGui.QAction.setActionGroup(group)
Parameters

groupPySide6.QtGui.QActionGroup

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.

PySide6.QtGui.QAction.setAutoRepeat(arg__1)
Parameters

arg__1 – bool

See also

autoRepeat()

PySide6.QtGui.QAction.setCheckable(arg__1)
Parameters

arg__1 – bool

See also

isCheckable()

PySide6.QtGui.QAction.setChecked(arg__1)
Parameters

arg__1 – bool

See also

isChecked()

PySide6.QtGui.QAction.setData(var)
Parameters

var – object

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

See also

data()

PySide6.QtGui.QAction.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.

PySide6.QtGui.QAction.setEnabled(arg__1)
Parameters

arg__1 – bool

See also

isEnabled()

PySide6.QtGui.QAction.setFont(font)
Parameters

fontPySide6.QtGui.QFont

See also

font()

PySide6.QtGui.QAction.setIcon(icon)
Parameters

iconPySide6.QtGui.QIcon

See also

icon()

PySide6.QtGui.QAction.setIconText(text)
Parameters

text – str

See also

iconText()

PySide6.QtGui.QAction.setIconVisibleInMenu(visible)
Parameters

visible – bool

PySide6.QtGui.QAction.setMenuRole(menuRole)
Parameters

menuRoleMenuRole

See also

menuRole()

PySide6.QtGui.QAction.setPriority(priority)
Parameters

priorityPriority

See also

priority()

PySide6.QtGui.QAction.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()

PySide6.QtGui.QAction.setShortcut(shortcut)
Parameters

shortcutPySide6.QtGui.QKeySequence

Sets shortcut as the sole shortcut that triggers the action.

PySide6.QtGui.QAction.setShortcutContext(context)
Parameters

contextShortcutContext

PySide6.QtGui.QAction.setShortcutVisibleInContextMenu(show)
Parameters

show – bool

PySide6.QtGui.QAction.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()

PySide6.QtGui.QAction.setShortcuts(shortcuts)
Parameters

shortcuts

PySide6.QtGui.QAction.setStatusTip(statusTip)
Parameters

statusTip – str

See also

statusTip()

PySide6.QtGui.QAction.setText(text)
Parameters

text – str

See also

text()

PySide6.QtGui.QAction.setToolTip(tip)
Parameters

tip – str

See also

toolTip()

PySide6.QtGui.QAction.setVisible(arg__1)
Parameters

arg__1 – bool

See also

isVisible()

PySide6.QtGui.QAction.setWhatsThis(what)
Parameters

what – str

See also

whatsThis()

PySide6.QtGui.QAction.shortcut()
Return type

PySide6.QtGui.QKeySequence

Returns the primary shortcut.

See also

setShortcuts()

PySide6.QtGui.QAction.shortcutContext()
Return type

ShortcutContext

PySide6.QtGui.QAction.shortcuts()
Return type

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

See also

setShortcuts()

PySide6.QtGui.QAction.showStatusText([object=None])
Parameters

objectPySide6.QtCore.QObject

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

PySide6.QtGui.QAction.statusTip()
Return type

str

See also

setStatusTip()

PySide6.QtGui.QAction.text()
Return type

str

See also

setText()

PySide6.QtGui.QAction.toggle()

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

PySide6.QtGui.QAction.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 .

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

See also

activate() triggered() checked

PySide6.QtGui.QAction.toolTip()
Return type

str

See also

setToolTip()

PySide6.QtGui.QAction.trigger()

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

PySide6.QtGui.QAction.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

PySide6.QtGui.QAction.visibleChanged()
PySide6.QtGui.QAction.whatsThis()
Return type

str

See also

setWhatsThis()