TQAction Class

Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# TQAction Class Reference

The TQAction class provides an abstract user interface action that can appear both in menus and tool bars. More...

`#include <tqaction.h>`

Inherits TQObject.

Inherited by TQActionGroup.

List of all member functions.

## Public Members

- TQAction ( TQObject * parent, const char * name = 0 )
- TQAction ( const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0 )
- TQAction ( const TQIconSet & icon, const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0 )
- TQAction ( const TQString & text, const TQIconSet & icon, const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0, bool toggle = false ) (obsolete)
- TQAction ( const TQString & text, const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0, bool toggle = false ) (obsolete)
- TQAction ( TQObject * parent, const char * name, bool toggle ) (obsolete)
- ~TQAction ()
- virtual void setIconSet ( const TQIconSet & )
- TQIconSet iconSet () const
- virtual void setText ( const TQString & )
- TQString text () const
- virtual void setMenuText ( const TQString & )
- TQString menuText () const
- virtual void setToolTip ( const TQString & )
- TQString toolTip () const
- virtual void setStatusTip ( const TQString & )
- TQString statusTip () const
- virtual void setWhatsThis ( const TQString & )
- TQString whatsThis () const
- virtual void setAccel ( const TQKeySequence & key )
- TQKeySequence accel () const
- virtual void setToggleAction ( bool )
- bool isToggleAction () const
- bool isOn () const
- bool isEnabled () const
- bool isVisible () const
- virtual bool addTo ( TQWidget * w )
- virtual bool removeFrom ( TQWidget * w )

## Public Slots

- void activate ()
- void toggle ()
- virtual void setOn ( bool )
- virtual void setEnabled ( bool )
- void setDisabled ( bool disable )
- void setVisible ( bool )

## Signals

- void activated ()
- void toggled ( bool on )

## Properties

- TQKeySequence accel - the action's accelerator key
- bool enabled - whether the action is enabled
- TQIconSet iconSet - the action's icon
- TQString menuText - the action's menu text
- bool on - whether a toggle action is on
- TQString statusTip - the action's status tip
- TQString text - the action's descriptive text
- bool toggleAction - whether the action is a toggle action
- TQString toolTip - the action's tool tip
- bool visible - whether the action can be seen (e.g. in menus and toolbars)
- TQString whatsThis - the action's "What's This?" help text

## Protected Members

- virtual void addedTo ( TQWidget * actionWidget, TQWidget * container )
- virtual void addedTo ( int index, TQPopupMenu * menu )

## Detailed Description

The TQAction class provides an abstract user interface action that can appear both in menus and tool bars.

In GUI applications many commands can be invoked via a menu option, a toolbar button and a keyboard accelerator. Since the same action must be performed regardless of how the action was invoked, and since the menu and toolbar should be kept in sync, it is useful to represent a command as an action. An action can be added to a menu and a toolbar and will automatically keep them in sync. For example, if the user presses a Bold toolbar button the Bold menu item will automatically be checked.

A TQAction may contain an icon, a menu text, an accelerator, a status text, a whats this text and a tool tip. Most of these can be set in the constructor. They can also be set independently with setIconSet(), setText(), setMenuText(), setToolTip(), setStatusTip(), setWhatsThis() and setAccel().

An action may be a toggle action e.g. a Bold toolbar button, or a command action, e.g. 'Open File' to invoke an open file dialog. Toggle actions emit the toggled() signal when their state changes. Both command and toggle actions emit the activated() signal when they are invoked. Use setToggleAction() to set an action's toggled status. To see if an action is a toggle action use isToggleAction(). A toggle action may be "on", isOn() returns true, or "off", isOn() returns false.

Actions are added to widgets (menus or toolbars) using addTo(), and removed using removeFrom().

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

            fileOpenAction = new TQAction( TQPixmap( fileopen ), "&Open...",
                                          CTRL+Key_O, this, "open" );
            connect( fileOpenAction, TQ_SIGNAL( activated() ) , this, TQ_SLOT( choose() ) );

We create a "File Save" action with a menu text of "&Save" and Ctrl+S as the keyboard accelerator. We connect the fileSaveAction's activated() signal to our own save() slot. Note that at this point there is no menu or toolbar action, we'll add them next:

            TQToolBar * fileTools = new TQToolBar( this, "file operations" );

            fileSaveAction->addTo( fileTools );

            TQPopupMenu * file = new TQPopupMenu( this );
            menuBar()->insertItem( "&File", file );

            fileSaveAction->addTo( file );

We create a toolbar and add our fileSaveAction to it. Similarly we create a menu, add a top-level menu item, and add our fileSaveAction.

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

To prevent recursion, don't create an action as a child of a widget that the action is later added to.

See also Main Window and Related Classes and Basic Widgets.

## Member Function Documentation

### TQAction::TQAction ( TQObject * parent, const char * name = 0 )

Constructs an action called name with parent parent.

If parent is a TQActionGroup, the new action inserts itself into parent.

For accelerators and status tips to work, parent must either be a widget, or an action group whose parent is a widget.

Warning: To prevent recursion, don't create an action as a child of a widget that the action is later added to.

### TQAction::TQAction ( const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0 )

This constructor results in an icon-less action with the the menu text menuText and keyboard accelerator accel. It is a child of parent and called name.

If parent is a TQActionGroup, the action automatically becomes a member of it.

For accelerators and status tips to work, parent must either be a widget, or an action group whose parent is a widget.

The action uses a stripped version of menuText (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 and accel will be used for tool tips and status tips unless you provide text for these using setToolTip() and setStatusTip().

Call setToggleAction(true) to make the action a toggle action.

Warning: To prevent recursion, don't create an action as a child of a widget that the action is later added to.

### TQAction::TQAction ( const TQIconSet & icon, const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0 )

This constructor creates an action with the following properties: the icon or iconset icon, the menu text menuText and keyboard accelerator accel. It is a child of parent and called name.

If parent is a TQActionGroup, the action automatically becomes a member of it.

For accelerators and status tips to work, parent must either be a widget, or an action group whose parent is a widget.

The action uses a stripped version of menuText (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 and accel will be used for tool tips and status tips unless you provide text for these using setToolTip() and setStatusTip().

Call setToggleAction(true) to make the action a toggle action.

Warning: To prevent recursion, don't create an action as a child of a widget that the action is later added to.

### TQAction::TQAction ( const TQString & text, const TQIconSet & icon, const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0, bool toggle = false )

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

This constructor creates an action with the following properties: the description text, the icon or iconset icon, the menu text menuText and keyboard accelerator accel. It is a child of parent and called name. If toggle is true the action will be a toggle action, otherwise it will be a command action.

If parent is a TQActionGroup, the action automatically becomes a member of it.

For accelerators and status tips to work, parent must either be a widget, or an action group whose parent is a widget.

The text and accel will be used for tool tips and status tips unless you provide specific text for these using setToolTip() and setStatusTip().

### TQAction::TQAction ( const TQString & text, const TQString & menuText, TQKeySequence accel, TQObject * parent, const char * name = 0, bool toggle = false )

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

This constructor results in an icon-less action with the description text, the menu text menuText and the keyboard accelerator accel. Its parent is parent and it is called name. If toggle is true the action will be a toggle action, otherwise it will be a command action.

The action automatically becomes a member of parent if parent is a TQActionGroup.

For accelerators and status tips to work, parent must either be a widget, or an action group whose parent is a widget.

The text and accel will be used for tool tips and status tips unless you provide specific text for these using setToolTip() and setStatusTip().

### TQAction::TQAction ( TQObject * parent, const char * name, bool toggle )

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Constructs an action called name with parent parent.

If toggle is true the action will be a toggle action, otherwise it will be a command action.

If parent is a TQActionGroup, the new action inserts itself into parent.

For accelerators and status tips to work, parent must either be a widget, or an action group whose parent is a widget.

### TQAction::~TQAction ()

Destroys the object and frees allocated resources.

### TQKeySequence TQAction::accel () const

Returns the action's accelerator key. See the "accel" property for details.

### void TQAction::activate ()`[slot]`

Activates the action and executes all connected slots. This only works for actions that are not toggle action.

See also toggle().

### void TQAction::activated ()`[signal]`

This signal is emitted when an action is activated by the user, e.g. when the user clicks a menu option or a toolbar button or presses an action's accelerator key combination.

Connect to this signal for command actions. Connect to the toggled() signal for toggle actions.

Examples: action/application.cpp, chart/chartform.cpp, and themes/themes.cpp.

### bool TQAction::addTo ( TQWidget * w )`[virtual]`

Adds this action to widget w.

Currently actions may be added to TQToolBar and TQPopupMenu widgets.

An action added to a tool bar is automatically displayed as a tool button; an action added to a pop up menu appears as a menu option.

addTo() returns true if the action was added successfully and false otherwise. (If w is not a TQToolBar or TQPopupMenu the action will not be added and false will be returned.)

See also removeFrom().

Examples: action/application.cpp, action/toggleaction/toggleaction.cpp, and chart/chartform.cpp.

Reimplemented in TQActionGroup.

### void TQAction::addedTo ( TQWidget * actionWidget, TQWidget * container )`[virtual protected]`

This function is called from the addTo() function when it has created a widget (actionWidget) for the action in the container.

### void TQAction::addedTo ( int index, TQPopupMenu * menu )`[virtual protected]`

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function is called from the addTo() function when it has created a menu item at the index position index in the popup menu menu.

### TQIconSet TQAction::iconSet () const

Returns the action's icon. See the "iconSet" property for details.

### bool TQAction::isEnabled () const

Returns true if the action is enabled; otherwise returns false. See the "enabled" property for details.

### bool TQAction::isOn () const

Returns true if a toggle action is on; otherwise returns false. See the "on" property for details.

### bool TQAction::isToggleAction () const

Returns true if the action is a toggle action; otherwise returns false. See the "toggleAction" property for details.

### bool TQAction::isVisible () const

Returns true if the action can be seen (e.g. in menus and toolbars); otherwise returns false. See the "visible" property for details.

### TQString TQAction::menuText () const

Returns the action's menu text. See the "menuText" property for details.

### bool TQAction::removeFrom ( TQWidget * w )`[virtual]`

Removes the action from widget w.

Returns true if the action was removed successfully; otherwise returns false.

See also addTo().

### void TQAction::setAccel ( const TQKeySequence & key )`[virtual]`

Sets the action's accelerator key to key. See the "accel" property for details.

### void TQAction::setDisabled ( bool disable )`[slot]`

Disables the action if disable is true; otherwise enables the action.

See the enabled documentation for more information.

### void TQAction::setEnabled ( bool )`[virtual slot]`

Sets whether the action is enabled. See the "enabled" property for details.

### void TQAction::setIconSet ( const TQIconSet & )`[virtual]`

Sets the action's icon. See the "iconSet" property for details.

### void TQAction::setMenuText ( const TQString & )`[virtual]`

Sets the action's menu text. See the "menuText" property for details.

### void TQAction::setOn ( bool )`[virtual slot]`

Sets whether a toggle action is on. See the "on" property for details.

### void TQAction::setStatusTip ( const TQString & )`[virtual]`

Sets the action's status tip. See the "statusTip" property for details.

### void TQAction::setText ( const TQString & )`[virtual]`

Sets the action's descriptive text. See the "text" property for details.

### void TQAction::setToggleAction ( bool )`[virtual]`

Sets whether the action is a toggle action. See the "toggleAction" property for details.

### void TQAction::setToolTip ( const TQString & )`[virtual]`

Sets the action's tool tip. See the "toolTip" property for details.

### void TQAction::setVisible ( bool )`[slot]`

Sets whether the action can be seen (e.g. in menus and toolbars). See the "visible" property for details.

### void TQAction::setWhatsThis ( const TQString & )`[virtual]`

Sets the action's "What's This?" help text. See the "whatsThis" property for details.

### TQString TQAction::statusTip () const

Returns the action's status tip. See the "statusTip" property for details.

### TQString TQAction::text () const

Returns the action's descriptive text. See the "text" property for details.

### void TQAction::toggle ()`[slot]`

Toggles the state of a toggle action.

See also on, activate(), toggled(), and toggleAction.

### void TQAction::toggled ( bool on )`[signal]`

This signal is emitted when a toggle action changes state; command actions and TQActionGroups don't emit toggled().

The on argument denotes the new state: If on is true the toggle action is switched on, and if on is false the toggle action is switched off.

To trigger a user command depending on whether a toggle action has been switched on or off connect it to a slot that takes a bool to indicate the state, e.g.

            TQMainWindow * window = new TQMainWindow;

            TQAction * labelonoffaction = new TQAction( window, "labelonoff" );

            TQObject::connect( labelonoffaction, TQ_SIGNAL( toggled( bool ) ),
                              window, TQ_SLOT( setUsesTextLabel( bool ) ) );

See also activated(), toggleAction, and on.

Example: action/toggleaction/toggleaction.cpp.

### TQString TQAction::toolTip () const

Returns the action's tool tip. See the "toolTip" property for details.

### TQString TQAction::whatsThis () const

Returns the action's "What's This?" help text. See the "whatsThis" property for details.

## Property Documentation

### TQKeySequence accel

This property holds the action's accelerator key.

The keycodes can be found in TQt::Key and TQt::Modifier. There is no default accelerator key.

Set this property's value with setAccel() and get this property's value with accel().

### bool enabled

This property holds whether the action is enabled.

Disabled actions can't be chosen by the user. They don't disappear from the menu/tool bar but are displayed in a way which indicates that they are unavailable, e.g. they might be displayed grayed out.

What's this? help on disabled actions is still available provided the TQAction::whatsThis property is set.

Set this property's value with setEnabled() and get this property's value with isEnabled().

### TQIconSet iconSet

This property holds the action's icon.

The icon is used as the tool button icon and in the menu to the left of the menu text. There is no default icon.

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

(See the action/toggleaction/toggleaction.cpp example.)

Set this property's value with setIconSet() and get this property's value with iconSet().

### TQString menuText

This property holds the action's menu text.

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

See also text.

Set this property's value with setMenuText() and get this property's value with menuText().

### bool on

This property holds whether a toggle action is on.

This property is always on (true) for command actions and TQActionGroups; setOn() has no effect on them. For action's where isToggleAction() is true, this property's default value is off (false).

See also toggleAction.

Set this property's value with setOn() and get this property's value with isOn().

### TQString statusTip

This property holds the action's status tip.

The statusTip is displayed on all status bars that this action's toplevel parent widget provides.

If no status tip is defined, the action uses the tool tip text.

There is no default statusTip text.

See also statusTip and toolTip.

Set this property's value with setStatusTip() and get this property's value with statusTip().

### TQString text

This property holds the action's descriptive text.

If TQMainWindow::usesTextLabel is true, the text appears as a label in the relevant tool button. It also serves as the default text in menus and tool tips if these have not been specifically defined. There is no default text.

See also menuText, toolTip, and statusTip.

Set this property's value with setText() and get this property's value with text().

### bool toggleAction

This property holds whether the action is a toggle action.

A toggle action is one which has an on/off state. For example a Bold toolbar button is 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. This property's default 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 TQActionGroup with the TQActionGroup::exclusive property set to true.

Set this property's value with setToggleAction() and get this property's value with isToggleAction().

### TQString toolTip

This property holds the action's tool tip.

This text is used for the tool tip. If no status tip has been set the tool tip will be used for the status tip.

If no tool tip is specified the action's text is used, and if that hasn't been specified the description text is used as the tool tip text.

There is no default tool tip text.

See also statusTip and accel.

Set this property's value with setToolTip() and get this property's value with toolTip().

### bool visible

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.

Set this property's value with setVisible() and get this property's value with isVisible().

### TQString whatsThis

This property holds the action's "What's This?" help text.

The whats this text is used to provide a brief description of the action. The text may contain rich text (HTML-like tags -- see TQStyleSheet for the list of supported tags). There is no default "What's This" text.

See also TQWhatsThis.

Set this property's value with setWhatsThis() and get this property's value with whatsThis().

This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.