Qt Reference Documentation

Q3Accel Class Reference

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

 #include <Q3Accel>

This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.

Inherits QObject.

Public Functions

Q3Accel ( QWidget * parent, const char * name = 0 )
Q3Accel ( QWidget * watch, QObject * parent, const char * name = 0 )
~Q3Accel ()
void clear ()
bool connectItem ( int id, const QObject * receiver, const char * member )
uint count () const
bool disconnectItem ( int id, const QObject * receiver, const char * member )
int findKey ( const QKeySequence & key ) const
int insertItem ( const QKeySequence & key, int id = -1 )
bool isEnabled () const
bool isItemEnabled ( int id ) const
QKeySequence key ( int id )
void removeItem ( int id )
void setEnabled ( bool enable )
void setItemEnabled ( int id, bool enable )
void setWhatsThis ( int id, const QString & text )
QString whatsThis ( int id ) const
  • 29 public functions inherited from QObject

Signals

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

Static Public Members

QKeySequence shortcutKey ( const QString & str )
  • 5 static public members inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 7 protected functions inherited from QObject

Detailed Description

The Q3Accel 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 QAction class to create actions with accelerators that can be used in both menus and toolbars. If you're only interested in menus use Q3MenuData::insertItem() or Q3MenuData::setAccel() to make accelerators for operations that are also available on menus. Many widgets automatically generate accelerators, such as QAbstractButton, QGroupBox, QLabel (with QLabel::setBuddy()), QMenuBar, and QTabBar. Example:

 QPushButton p("&Exit", parent); // automatic shortcut Alt+E
 Q3PopupMenu *fileMenu = new fileMenu(parent);
 fileMenu->insertItem("Undo", parent, SLOT(undo()),
                      Qt::CTRL + Qt::Key_Z);

A Q3Accel 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 QKeySequence. A single key sequence consists of a keyboard code combined with modifiers (Qt::SHIFT, Qt::CTRL, Qt::ALT, or Qt::UNICODE_ACCEL). For example, Qt::CTRL + Qt::Key_P could be a shortcut for printing a document. As an alternative, use Qt::UNICODE_ACCEL with the unicode code point of the character. For example, Qt::UNICODE_ACCEL + 'A' gives the same accelerator as Qt::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 QPushButton and QCheckBox connect the activatedAmbiguously() signal to the harmless setFocus() slot, whereas activated() is connected to a slot invoking the button's action. Most controls, like QLabel and QTabBar, 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 Q3Accel 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(QKeySequence("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:

 Q3Accel *a = new Q3Accel(myWindow);
 a->connectItem(a->insertItem(Qt::CTRL + Qt::Key_P),
                myWindow, SLOT(printDoc()));

QAbstractButton::setAccel() QLabel::setBuddy() QKeySequence

See also QKeyEvent and QWidget::keyPressEvent().

Member Function Documentation

Q3Accel::Q3Accel ( QWidget * parent, const char * name = 0 )

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

Q3Accel::Q3Accel ( QWidget * watch, QObject * parent, const char * name = 0 )

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

This constructor is not needed for normal application programming.

Q3Accel::~Q3Accel ()

Destroys the accelerator object and frees all allocated resources.

void Q3Accel::activated ( int id ) [signal]

This signal is emitted when the user types the shortcut's key sequence. id is a number that identifies this particular accelerator item.

See also activatedAmbiguously().

void Q3Accel::activatedAmbiguously ( int id ) [signal]

This signal is emitted when the user types a shortcut key sequence that is ambiguous. For example, if one key sequence is a "prefix" for another and the user types these keys it isn't clear if they want the shorter key sequence, or if they're about to type more to complete the longer key sequence. id is a number that identifies this particular accelerator item.

See also activated().

void Q3Accel::clear ()

Removes all accelerator items.

bool Q3Accel::connectItem ( int id, const QObject * receiver, const char * member )

Connects the accelerator item id to the slot member of receiver. Returns true if the connection is successful.

 a->connectItem(201, mainView, 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() and QObject::connect().

uint Q3Accel::count () const

Returns the number of accelerator items in this accelerator.

bool Q3Accel::disconnectItem ( int id, const QObject * receiver, const char * member )

Disconnects the accelerator item identified by id from the function called member in the receiver object. Returns true if the connection existed and the disconnect was successful.

See also connectItem() and QObject::disconnect().

int Q3Accel::findKey ( const QKeySequence & key ) const

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

int Q3Accel::insertItem ( const QKeySequence & 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.

 Q3Accel *a = new Q3Accel(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

bool Q3Accel::isEnabled () const

Returns true if the accelerator is enabled; otherwise returns false.

See also setEnabled() and isItemEnabled().

bool Q3Accel::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().

QKeySequence Q3Accel::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.

void Q3Accel::removeItem ( int id )

Removes the accelerator item with the identifier id.

void Q3Accel::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 Q3Accel.

See also isEnabled() and setItemEnabled().

void Q3Accel::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 Q3Accel.

See also isItemEnabled() and isEnabled().

void Q3Accel::setWhatsThis ( int id, const QString & 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 Q3MenuData::setWhatsThis().

See also whatsThis(), QWhatsThis::inWhatsThisMode(), and QAction::setWhatsThis().

QKeySequence Q3Accel::shortcutKey ( const QString & 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 QKeySequence(Qt::ALT + Qt::Key_X), shortcutKey("&Quit") returns QKeySequence(Qt::ALT + Qt::Key_Q), and shortcutKey("Quit") returns QKeySequence().

QString Q3Accel::whatsThis ( int id ) const

Returns the What's This help text for the specified item id or an empty string if no text has been specified.

See also setWhatsThis().

X

Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.