class KAction

Class to encapsulate user-driven action or event. More...

Definition#include <kstdaction.h>
InheritsQAction (qk)
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Methods

Protected Methods

Protected Slots


Detailed Description

The KAction class (and derived and super classes) provide a way to easily encapsulate a "real" user-selected action or event in your program. For instance, a user may want to "paste" the contents of the clipboard or "scroll down" a document or "quit" the application. These are all @bf actions -- events that the user causes to happen. The KAction class allows the developer to deal with this actions in an easy and intuitive manner.

Specifically, the KAction class encapsulated the various attributes to an event/action. For instance, an action might have an icon that goes along with it (a clipboard for a "paste" action or scissors for a "cut" action). The action might have some text to describe the action. It will certainly have a method or function that actually @bf executes the action! All these attributes are contained within the KAction object.

The advantage of dealing with Actions is that you can manipulate the Action without regard to the GUI representation of it. For instance, in the "normal" way of dealing with actions like "cut", you would manually insert a item for Cut into a menu and a button into a toolbar. If you want to disable the cut action for a moment (maybe nothing is selected), you woud have to hunt down the pointer to the menu item and the toolbar button and disable both individually. Setting the menu item and toolbar item up uses very similar code.. but has to be done twice!

With the Action concept, you simply "plug" the Action into whatever GUI element you want. The KAction class will then take care of correctly defining the menu item (with icons, accelerators, text, etc) or toolbar button.. or whatever. From then on, if you manipulate the Action at all, the effect will propogate through all GUI representations of it. Back to the "cut" example: if you want to disable the Cut Action, you would simply do 'cutAction->setEnabled(false)' and the menuitem and button would instantly be disabled!

This is the biggest advantage to the Action concept -- there is a one-to-one relationship between the "real" action and all GUI representations of it.

General Usage:

The steps to using actions are roughly as follows

Detailed Example:

Here is an example of enabling a "New [document]" action


 KAction *newAct = new KAction(i18n("&New"), QIconSet(BarIcon("filenew")),
								 KStdAccel::key(KStdAccel::New), this, SLOT(fileNew()),
								 this);

This line creates our action. It says that wherever this action is displayed, it will use "&New" as the text, the standard icon, and the standard accelerator. It further says that whenever this action is invoked, it will use the fileNew() slot to execute it.


 QPopupMenu *file = new QPopupMenu;
 newAct->plug(file);

That just inserted the action into the File menu. You can totally forget about that! In the future, all manipulation of the item is done through the newAct object.


 newAct->plug(toolBar());

And this inserted the Action into the main toolbar as a button.

That's it!

If you want to disable that action sometime later, you can do so with


 newAct->setEnabled(false)

and both the menuitem in File and the toolbar button will instantly be disabled.

Note: if you are using a "standard" action like "new", "paste", "quit", or any other action described in the KDE UI Standards, please use the methods in the KStdAction class rather then defining your own.

KAction ( const QString& text, int accel = 0, QObject* parent = 0, const char* name = 0 )

Construct an action with text and potential keyboard accelerator but nothing else. Use this only if you really know what you are doing.

Parameters:
textThe text that will be displayed.
accelThe corresponding keyboard accelerator (shortcut).
parentThis action's parent.
nameAn internal name for this action.

KAction ( const QString& text, int accel, const QObject* receiver, const char* slot, QObject* parent, const char* name = 0 )

Construct an action with text, potential keyboard accelerator, and a SLOT to call when this action is invoked by the user.

If you do not want or have a keyboard accelerator, set the accel param to 0.

This is the most common KAction used when you do not have a corresponding icon.

Parameters:
textThe text that will be displayed.
accelThe corresponding keyboard accelerator (shortcut).
receiverThe SLOT's parent.
slotThe SLOT to invoke to execute this action.
parentThis action's parent.
nameAn internal name for this action.

KAction ( const QString& text, const QIconSet& pix, int accel = 0, QObject* parent = 0, const char* name = 0 )

Construct an action with text, icon, and a potential keyboard accelerator.

This Action cannot execute any command. Use this only if you really know what you are doing.

Parameters:
textThe text that will be displayed.
pixThe icons that go with this action.
accelThe corresponding keyboard accelerator (shortcut).
parentThis action's parent.
nameAn internal name for this action.

KAction ( const QString& text, const QIconSet& pix, int accel, const QObject* receiver, const char* slot, QObject* parent, const char* name = 0 )

Construct an action with text, icon, potential keyboard accelerator, and a SLOT to call when this action is invoked by the user.

If you do not want or have a keyboard accelerator, set the accel param to 0.

This is the other common KAction used. Use it when you @bf do have a corresponding icon.

Parameters:
textThe text that will be displayed.
pixThe icon to display.
accelThe corresponding keyboard accelerator (shortcut).
receiverThe SLOT's parent.
slotThe SLOT to invoke to execute this action.
parentThis action's parent.
nameAn internal name for this action.

KAction ( QObject* parent = 0, const char* name = 0 )

Construct a null action.

Parameters:
parentThis action's parent.
nameAn internal name for this action.

int plug ( QWidget *w, int index = -1 )
[virtual]

"Plug" or insert this action into a given widget.

This will typically be a menu or a toolbar. From this point on, you will never need to directly manipulate the item in the menu or toolbar. You do all enabling/disabling/manipulation directly with your KAction object.

Parameters:
wThe GUI element to display this action

void unplug ( QWidget *w )
[virtual]

"Unplug" or remove this action from a given widget.

This will typically be a menu or a toolbar. This is rarely used in "normal" application. Typically, it would be used if your application has several views or modes, each with a completely different menu structure. If you simply want to disable an action for a given period, use setEnabled() instead.

Parameters:
wRemove the action from this GUI element.


Generated by: root@tantive.terraplex.com on Sun Feb 27 17:39:25 2000, using kdoc 2.0a33.