Home · All Classes · Modules

QAction Class Reference
[QtGui module]

The QAction class provides an abstract user interface action that can be inserted into widgets. More...

Inherits QObject.

Types

Methods

Qt Signals


Detailed Description

The QAction class provides an abstract user interface action that can be inserted into widgets.

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.

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.

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

Actions are added to widgets using QWidget.addAction().

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:

        openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
        openAct->setShortcut(tr("Ctrl+O"));
        openAct->setStatusTip(tr("Open an existing file"));
        connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

        fileMenu->addAction(openAct);
        fileToolBar->addAction(openAct);

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.


Type Documentation

QAction.ActionEvent

This enum type is used when calling QAction.activate()

Constant Value Description
QAction.Trigger 0 this will cause the QAction.triggered() signal to be emitted.
QAction.Hover 1 this will cause the QAction.hovered() signal to be emitted.



Method Documentation

QAction.__init__ (self, QObject parent)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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

QAction.__init__ (self, QString text, QObject parent)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs an action with 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 toolbuttons. You can override this by setting a specific description with setText(). The same text will be used for tooltips unless you specify a different test using setToolTip().

QAction.__init__ (self, QIcon icon, QString text, QObject parent)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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 toolbuttons. You can override this by setting a specific description with setText(). The same text will be used for tooltips unless you specify a different test using setToolTip().

QActionGroup QAction.actionGroup (self)

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

See also QActionGroup and QAction.setActionGroup().

QAction.activate (self, ActionEvent event)

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.

QVariant QAction.data (self)

Returns the user data as set in QAction.setData.

See also setData().

bool QAction.event (self, QEvent)

QFont QAction.font (self)

QAction.hover (self)

This method is also a Qt slot with the C++ signature void hover().

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

QIcon QAction.icon (self)

QString QAction.iconText (self)

bool QAction.isCheckable (self)

bool QAction.isChecked (self)

bool QAction.isEnabled (self)

bool QAction.isSeparator (self)

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

See also QAction.setSeparator().

bool QAction.isVisible (self)

QMenu QAction.menu (self)

Returns the menu contained by this action. Actions that contain menus can be used to create menu items with submenus, or inserted into toolbars to create buttons with popup menus.

See also setMenu() and QMenu.addAction().

QWidget QAction.parentWidget (self)

Returns the parent widget.

QAction.setActionGroup (self, QActionGroup group)

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.

See also QActionGroup and QAction.actionGroup().

QAction.setCheckable (self, bool)

QAction.setChecked (self, bool)

This method is also a Qt slot with the C++ signature void setChecked(bool).

QAction.setData (self, QVariant var)

Sets internal data to data. This can be used for user data to store anything that a QVariant can store. The ownership of anything the the user data will remain with the variant and thus be referenced counted as appropriate.

See also data().

QAction.setDisabled (self, bool b)

This method is also a Qt slot with the C++ signature void setDisabled(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.

QAction.setEnabled (self, bool)

This method is also a Qt slot with the C++ signature void setEnabled(bool).

QAction.setFont (self, QFont font)

QAction.setIcon (self, QIcon icon)

QAction.setIconText (self, QString text)

QAction.setMenu (self, QMenu menu)

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

See also menu().

QAction.setSeparator (self, bool b)

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

QAction.setShortcut (self, QKeySequence shortcut)

QAction.setShortcutContext (self, Qt.ShortcutContext context)

QAction.setStatusTip (self, QString statusTip)

QAction.setText (self, QString text)

QAction.setToolTip (self, QString tip)

QAction.setVisible (self, bool)

This method is also a Qt slot with the C++ signature void setVisible(bool).

QAction.setWhatsThis (self, QString what)

QKeySequence QAction.shortcut (self)

Qt.ShortcutContext QAction.shortcutContext (self)

bool QAction.showStatusText (self, QWidget widget = None)

Updates the status bar for widget. If widget is an appropriate QStatusBar found for for this action based on the parent heirarchy will be used.

See also statusTip.

QString QAction.statusTip (self)

QString QAction.text (self)

QAction.toggle (self)

This method is also a Qt slot with the C++ signature void toggle().

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

QString QAction.toolTip (self)

QAction.trigger (self)

This method is also a Qt slot with the C++ signature void trigger().

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

QString QAction.whatsThis (self)


Qt Signal Documentation

void 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 QWidget.actionEvent().

void 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 QAction.activate().

void toggled (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.

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

See also QAction.activate(), QAction.triggered(), and checked.

void triggered (bool = 0)

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 QAction.activate(), QAction.toggled(), and checked.


PyQt 4.0.1 for X11Copyright © Riverbank Computing Ltd and Trolltech AS 2006Qt 4.1.4