TQAccel Class

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

# TQAccel Class Reference

The TQAccel class handles keyboard accelerator and shortcut keys. More...

`#include <ntqaccel.h>`

Inherits TQObject.

List of all member functions.

## Public Members

- TQAccel ( TQWidget * parent, const char * name = 0 )
- TQAccel ( TQWidget * watch, TQObject * parent, const char * name = 0 )
- ~TQAccel ()
- bool isEnabled () const
- void setEnabled ( bool enable )
- uint count () const
- int insertItem ( const TQKeySequence & key, int id = -1 )
- void removeItem ( int id )
- void clear ()
- TQKeySequence key ( int id )
- int findKey ( const TQKeySequence & key ) const
- bool isItemEnabled ( int id ) const
- void setItemEnabled ( int id, bool enable )
- bool connectItem ( int id, const TQObject * receiver, const char * member )
- bool disconnectItem ( int id, const TQObject * receiver, const char * member )
- void repairEventFilter () (obsolete)
- void setWhatsThis ( int id, const TQString & text )
- TQString whatsThis ( int id ) const

## Signals

- void activated ( int id )
- void activatedAmbiguously ( int id )

## Static Public Members

- TQKeySequence shortcutKey ( const TQString & str )
- TQString keyToString ( TQKeySequence k ) (obsolete)
- TQKeySequence stringToKey ( const TQString & s ) (obsolete)

## Protected Members

- virtual bool eventFilter ( TQObject *, TQEvent * ) (obsolete)

## Detailed Description

The TQAccel class handles keyboard accelerator and shortcut keys.

A keyboard accelerator triggers an action when a certain key combination is pressed. The accelerator handles all keyboard activity for all the children of one top-level widget, so it is not affected by the keyboard focus.

In most cases, you will not need to use this class directly. Use the TQAction class to create actions with accelerators that can be used in both menus and toolbars. If you're only interested in menus use TQMenuData::insertItem() or TQMenuData::setAccel() to make accelerators for operations that are also available on menus. Many widgets automatically generate accelerators, such as TQButton, TQGroupBox, TQLabel (with TQLabel::setBuddy()), TQMenuBar and TQTabBar. Example:

            TQPushButton p( "&Exit", parent ); // automatic shortcut ALT+Key_E
            TQPopupMenu *fileMenu = new fileMenu( parent );
            fileMenu->insertItem( "Undo", parent, TQ_SLOT(undo()), CTRL+Key_Z );

A TQAccel contains a list of accelerator items that can be manipulated using insertItem(), removeItem(), clear(), key() and findKey().

Each accelerator item consists of an identifier and a TQKeySequence. A single key sequence consists of a keyboard code combined with modifiers (SHIFT, CTRL, ALT or UNICODE_ACCEL). For example, `CTRL + Key_P` could be a shortcut for printing a document. The key codes are listed in ntqnamespace.h. As an alternative, use UNICODE_ACCEL with the unicode code point of the character. For example, `UNICODE_ACCEL + 'A'` gives the same accelerator as Key_A.

When an accelerator key is pressed, the accelerator sends out the signal activated() with a number that identifies this particular accelerator item. Accelerator items can also be individually connected, so that two different keys will activate two different slots (see connectItem() and disconnectItem()).

The activated() signal is not emitted when two or more accelerators match the same key. Instead, the first matching accelerator sends out the activatedAmbiguously() signal. By pressing the key multiple times, users can navigate between all matching accelerators. Some standard controls like TQPushButton and TQCheckBox connect the activatedAmbiguously() signal to the harmless setFocus() slot, whereas activated() is connected to a slot invoking the button's action. Most controls, like TQLabel and TQTabBar, treat activated() and activatedAmbiguously() as equivalent.

Use setEnabled() to enable or disable all the items in an accelerator, or setItemEnabled() to enable or disable individual items. An item is active only when both the TQAccel and the item itself are enabled.

The function setWhatsThis() specifies a help text that appears when the user presses an accelerator key in What's This mode.

The accelerator will be deleted when parent is deleted, and will consume relevant key events until then.

Please note that the accelerator

            accelerator->insertItem( TQKeySequence("M") );

can be triggered with both the 'M' key, and with Shift+M, unless a second accelerator is defined for the Shift+M combination.

Example:

            TQAccel *a = new TQAccel( myWindow );        // create accels for myWindow
            a->connectItem( a->insertItem(Key_P+CTRL), // adds Ctrl+P accelerator
                            myWindow,                  // connected to myWindow's
                            TQ_SLOT(printDoc()) );        // printDoc() slot

See also TQKeyEvent, TQWidget::keyPressEvent(), TQMenuData::setAccel(), TQButton::accel, TQLabel::setBuddy(), TQKeySequence, GUI Design Handbook: Keyboard Shortcuts, and Miscellaneous Classes.

## Member Function Documentation

### TQAccel::TQAccel ( TQWidget * parent, const char * name = 0 )

Constructs a TQAccel object called name, with parent parent. The accelerator operates on parent.

### TQAccel::TQAccel ( TQWidget * watch, TQObject * parent, const char * name = 0 )

Constructs a TQAccel object called name, that operates on watch, and is a child of parent.

This constructor is not needed for normal application programming.

### TQAccel::~TQAccel ()

Destroys the accelerator object and frees all allocated resources.

### void TQAccel::activated ( int id )`[signal]`

This signal is emitted when an accelerator key is pressed. id is a number that identifies this particular accelerator item.

See also activatedAmbiguously().

### void TQAccel::activatedAmbiguously ( int id )`[signal]`

This signal is emitted when an accelerator key is pressed. id is a number that identifies this particular accelerator item.

See also activated().

### void TQAccel::clear ()

Removes all accelerator items.

### bool TQAccel::connectItem ( int id, const TQObject * receiver, const char * member )

Connects the accelerator item id to the slot member of receiver.

            a->connectItem( 201, mainView, TQ_SLOT(quit()) );

Of course, you can also send a signal as member.

Normally accelerators are connected to slots which then receive the `activated(int id)` signal with the id of the accelerator item that was activated. If you choose to connect a specific accelerator item using this function, the activated() signal is emitted if the associated key sequence is pressed but no `activated(int id)` signal is emitted.

See also disconnectItem().

Example: t14/gamebrd.cpp.

### uint TQAccel::count () const

Returns the number of accelerator items in this accelerator.

### bool TQAccel::disconnectItem ( int id, const TQObject * receiver, const char * member )

Disconnects an accelerator item with id id from the function called member in the receiver object.

See also connectItem().

### bool TQAccel::eventFilter ( TQObject *, TQEvent * )`[virtual protected]`

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

Reimplemented from TQObject.

### int TQAccel::findKey ( const TQKeySequence & key ) const

Returns the identifier of the accelerator item with the key code key, or -1 if the item cannot be found.

### int TQAccel::insertItem ( const TQKeySequence & key, int id = -1 )

Inserts an accelerator item and returns the item's identifier.

key is a key code and an optional combination of SHIFT, CTRL and ALT. id is the accelerator item id.

If id is negative, then the item will be assigned a unique negative identifier less than -1.

            TQAccel *a = new TQAccel( myWindow );        // create accels for myWindow
            a->insertItem( CTRL + Key_P, 200 );        // Ctrl+P, e.g. to print document
            a->insertItem( ALT + Key_X, 201 );         // Alt+X, e.g. to quit
            a->insertItem( UNICODE_ACCEL + 'q', 202 ); // Unicode 'q', e.g. to quit
            a->insertItem( Key_D );                    // gets a unique negative id < -1
            a->insertItem( CTRL + SHIFT + Key_P );     // gets a unique negative id < -1

Example: t14/gamebrd.cpp.

### bool TQAccel::isEnabled () const

Returns TRUE if the accelerator is enabled; otherwise returns FALSE.

See also setEnabled() and isItemEnabled().

### bool TQAccel::isItemEnabled ( int id ) const

Returns TRUE if the accelerator item with the identifier id is enabled. Returns FALSE if the item is disabled or cannot be found.

See also setItemEnabled() and isEnabled().

### TQKeySequence TQAccel::key ( int id )

Returns the key sequence of the accelerator item with identifier id, or an invalid key sequence (0) if the id cannot be found.

### TQString TQAccel::keyToString ( TQKeySequence k )`[static]`

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

Creates an accelerator string for the key k. For instance CTRL+Key_O gives "Ctrl+O". The "Ctrl" etc. are translated (using TQObject::tr()) in the "TQAccel" context.

The function is superfluous. Cast the TQKeySequence k to a TQString for the same effect.

### void TQAccel::removeItem ( int id )

Removes the accelerator item with the identifier id.

### void TQAccel::repairEventFilter ()

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

### void TQAccel::setEnabled ( bool enable )

Enables the accelerator if enable is TRUE, or disables it if enable is FALSE.

Individual keys can also be enabled or disabled using setItemEnabled(). To work, a key must be an enabled item in an enabled TQAccel.

See also isEnabled() and setItemEnabled().

### void TQAccel::setItemEnabled ( int id, bool enable )

Enables the accelerator item with the identifier id if enable is TRUE, and disables item id if enable is FALSE.

To work, an item must be enabled and be in an enabled TQAccel.

See also isItemEnabled() and isEnabled().

### void TQAccel::setWhatsThis ( int id, const TQString & text )

Sets a What's This help text for the accelerator item id to text.

The text will be shown when the application is in What's This mode and the user hits the accelerator key.

To set What's This help on a menu item (with or without an accelerator key), use TQMenuData::setWhatsThis().

See also whatsThis(), TQWhatsThis::inWhatsThisMode(), TQMenuData::setWhatsThis(), and TQAction::whatsThis.

### TQKeySequence TQAccel::shortcutKey ( const TQString & str )`[static]`

Returns the shortcut key sequence for str, or an invalid key sequence (0) if str has no shortcut sequence.

For example, shortcutKey("E&xit") returns ALT+Key_X, shortcutKey("&Quit") returns ALT+Key_Q and shortcutKey("Quit") returns 0. (In code that does not inherit the TQt namespace class, you must write e.g. TQt::ALT+TQt::Key_Q.)

We provide a list of common accelerators in English. At the time of writing, Microsoft and Open Group do not appear to have issued equivalent recommendations for other languages.

### TQKeySequence TQAccel::stringToKey ( const TQString & s )`[static]`

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

Returns an accelerator code for the string s. For example "Ctrl+O" gives CTRL+UNICODE_ACCEL+'O'. The strings "Ctrl", "Shift", "Alt" are recognized, as well as their translated equivalents in the "TQAccel" context (using TQObject::tr()). Returns 0 if s is not recognized.

This function is typically used with tr(), so that accelerator keys can be replaced in translations:

        TQPopupMenu *file = new TQPopupMenu( this );
        file->insertItem( p1, tr("&Open..."), this, TQ_SLOT(open()),
                          TQAccel::stringToKey(tr("Ctrl+O", "File|Open")) );

Notice the `"File|Open"` translator comment. It is by no means necessary, but it provides some context for the human translator.

The function is superfluous. Construct a TQKeySequence from the string s for the same effect.

See also TQObject::tr() and Internationalization with TQt.

Example: i18n/mywidget.cpp.

### TQString TQAccel::whatsThis ( int id ) const

Returns the What's This help text for the specified item id or TQString::null if no text has been specified.

See also setWhatsThis().

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