# QMenuData Class Reference

The QMenuData class is a base class for QMenuBar and QPopupMenu. More...

`#include <qmenudata.h>`

Inherited by QMenuBar and QPopupMenu.

List of all member functions.

## Public Members

- QMenuData ()
- virtual ~QMenuData ()
- uint count () const
- int insertItem ( const char * text, const QObject * receiver, const char * member, int accel=0 )
- int insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, int accel=0 )
- int insertItem ( const QPixmap & pixmap, const char * text, const QObject * receiver, const char * member, int accel=0 )
- int insertItem ( const char * text, const QObject * receiver, const char * member, int accel, int id, int index = -1 )
- int insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, int accel, int id, int index = -1 )
- int insertItem ( const QPixmap & pixmap, const char * text, const QObject * receiver, const char * member, int accel, int id, int index = -1 )
- int insertItem ( const char * text, int id=-1, int index=-1 )
- int insertItem ( const char * text, QPopupMenu * popup, int id=-1, int index=-1 )
- int insertItem ( const QPixmap & pixmap, int id=-1, int index=-1 )
- int insertItem ( const QPixmap & pixmap, QPopupMenu * popup, int id=-1, int index=-1 )
- int insertItem ( const QPixmap & pixmap, const char * text, int id=-1, int index=-1 )
- int insertItem ( const QPixmap & pixmap, const char * text, QPopupMenu * popup, int id=-1, int index=-1 )
- void insertSeparator ( int index=-1 )
- void removeItem ( int id )
- void removeItemAt ( int index )
- void clear ()
- int accel ( int id ) const
- void setAccel ( int key, int id )
- const char* text ( int id ) const
- QPixmap* pixmap ( int id ) const
- void changeItem ( const char * text, int id )
- void changeItem ( const QPixmap & pixmap, int id )
- void changeItem ( const QPixmap & pixmap, const char * text, int id )
- bool isItemEnabled ( int id ) const
- void setItemEnabled ( int id, bool enable )
- bool isItemChecked ( int id ) const
- void setItemChecked ( int id, bool check )
- virtual void updateItem ( int id )
- int indexOf ( int id ) const
- int idAt ( int index ) const
- void setId ( int index, int id )
- bool connectItem ( int id, const QObject * receiver, const char * member )
- bool disconnectItem ( int id, const QObject * receiver, const char * member )
- QMenuItem* findItem ( int id ) const
- QMenuItem* findItem ( int id, QMenuData ** parent ) const

## Protected Members

- virtual void menuContentsChanged ()
- virtual void menuStateChanged ()
- virtual void menuInsPopup ( QPopupMenu * )
- virtual void menuDelPopup ( QPopupMenu * )
- QMenuItem* findPopup ( QPopupMenu *, int * index = 0 )

## Detailed Description

The QMenuData class is a base class for QMenuBar and QPopupMenu.

QMenuData has an internal list of menu items. A menu item is a text, pixmap or a separator, and may also have a popup menu (separators have no popup menus).

The menu item sends out an activated() signal when it is selected, and a highlighted() signal when it receives the user input focus.

Menu items can be accessed through identifiers.

See also: QAccel.

## Member Function Documentation

### QMenuData::QMenuData ()

Constructs an empty list.

### QMenuData::~QMenuData () `[virtual]`

Removes all menu items and disconnects any signals that have been connected.

### int QMenuData::accel ( int id ) const

Returns the accelerator key that has been defined for the menu item id, or 0 if it has no accelerator key.

See also: setAccel(), QAccel and qkeycode.h.

### void QMenuData::changeItem ( const QPixmap & pixmap, const char * text, int id )

Changes the pixmap and text of the menu item id.

See also: pixmap().

### void QMenuData::changeItem ( const QPixmap & pixmap, int id )

Changes the pixmap of the menu item id.

See also: pixmap().

### void QMenuData::changeItem ( const char * text, int id )

Changes the text of the menu item id.

See also: text().

### void QMenuData::clear ()

Removes all menu items.

See also: removeItem() and removeItemAt().

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

Connects a menu item to a receiver and a slot or signal.

The receiver's slot/signal is activated when the menu item is activated.

### uint QMenuData::count () const

Returns the number of items in the menu.

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

Disconnects a receiver/member from a menu item.

All connections are removed when the menu data object is destroyed.

### QMenuItem * QMenuData::findItem ( int id ) const

Returns a pointer to the menu item with identifier id, or 0 if there is no item with such an identifier.

See also: indexOf().

### QMenuItem * QMenuData::findItem ( int id, QMenuData ** parent ) const

Returns a pointer to the menu item with identifier id, or 0 if there is no item with such an identifier, and changes parent to point to the parent of the return value.

See also: indexOf().

### QMenuItem * QMenuData::findPopup ( QPopupMenu * popup, int * index = 0 ) `[protected]`

Internal function that finds the menu item where popup is located, storing its index at index if index is not NULL.

### int QMenuData::idAt ( int index ) const

Returns the identifier of the menu item at position index in the internal list, or -1 if index is out of range.

See also: setId() and indexOf().

### int QMenuData::indexOf ( int id ) const

Returns the index of the menu item with identifier id, or -1 if there is no item with such an identifier.

See also: idAt() and findItem().

### int QMenuData::insertItem ( const QPixmap & pixmap, QPopupMenu * popup, int id=-1, int index=-1 )

Inserts a menu item with a pixmap and a sub menu. Returns the menu item identifier.

The popup must be deleted by the programmer. It is not deleted when this menu item is removed or when the menu is deleted. Note that all popups are automatically deleted when the application terminates. (The QApplication destructor destroys all remaining widgets.)

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel() and connectItem().

### int QMenuData::insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, int accel, int id, int index = -1 )

Inserts a menu item with a pixmap, an accelerator key, an id and an optional index and connects it to an object/slot.

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Note that accelerators only work for QPopupMenu items that live in a menu bar. For stand-alone popup menus, use an independent QAccel object.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel(), connectItem(), QAccel and qkeycode.h.

### int QMenuData::insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, int accel=0 )

Inserts a menu item with a pixmap and an optional accelerator key, and connects it to an object/slot.

Returns a unique menu item identifier (negative integer <= -2).

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel(), connectItem(), QAccel and qkeycode.h.

### int QMenuData::insertItem ( const QPixmap & pixmap, const char * text, QPopupMenu * popup, int id=-1, int index=-1 )

Inserts a menu item with a pixmap, a text and a sub menu. Returns the menu item identifier.

The popup must be deleted by the programmer. It is not deleted when this menu item is removed or when the menu is deleted. Note that all popups are automatically deleted when the application terminates. (The QApplication destructor destroys all remaining widgets.)

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel() and connectItem().

### int QMenuData::insertItem ( const QPixmap & pixmap, const char * text, const QObject * receiver, const char * member, int accel, int id, int index = -1 )

Inserts a menu item with a pixmap, a text, an accelerator key, an id and an optional index and connects it to an object/slot.

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Note that accelerators only work for QPopupMenu items that live in a menu bar. For stand-alone popup menus, use an independent QAccel object.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel(), connectItem(), QAccel and qkeycode.h.

Examples: qtimage/qtimage.cpp grapher/grapher.cpp xform/xform.cpp application/application.cpp menu/menu.cpp progress/progress.cpp layout/layout.cpp showimg/showimg.cpp widgets/widgets.cpp

### int QMenuData::insertItem ( const QPixmap & pixmap, const char * text, const QObject * receiver, const char * member, int accel=0 )

Inserts a menu item with a pixmap, a text and an optional accelerator key, and connects it to an object/slot. The pixmap will be displayed to the left of the text in the item.

Returns a unique menu item identifier (negative integer <= -2).

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel(), connectItem(), QAccel and qkeycode.h.

### int QMenuData::insertItem ( const QPixmap & pixmap, const char * text, int id=-1, int index=-1 )

Inserts a menu item with a pixmap and a text. Returns the menu item identifier.

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel() and connectItem().

### int QMenuData::insertItem ( const QPixmap & pixmap, int id=-1, int index=-1 )

Inserts a menu item with a pixmap. Returns the menu item identifier.

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel() and connectItem().

### int QMenuData::insertItem ( const char * text, QPopupMenu * popup, int id=-1, int index=-1 )

Inserts a menu item with a text and a sub menu. Returns the menu item identifier.

The popup must be deleted by the programmer. It is not deleted when this menu item is removed or when the menu is deleted. Note that all popups are automatically deleted when the application terminates. (The QApplication destructor destroys all remaining widgets.)

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel() and connectItem().

### int QMenuData::insertItem ( const char * text, const QObject * receiver, const char * member, int accel, int id, int index = -1 )

Inserts a menu item with a text, an accelerator key, an id and an optional index and connects it to an object/slot.

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Note that accelerators only work for QPopupMenu items that live in a menu bar. For stand-alone popup menus, use an independent QAccel object.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel(), connectItem(), QAccel and qkeycode.h.

### int QMenuData::insertItem ( const char * text, const QObject * receiver, const char * member, int accel=0 )

Inserts a menu item with a text and an optional accelerator key, and connects it to an object/slot.

Returns a unique menu item identifier (negative integer <= -2).

Example:

        QMenuBar   *mainMenu = new QMenuBar;
        QPopupMenu *fileMenu = new QPopupMenu;
        fileMenu->insertItem( "New",  myView, SLOT(newFile()), CTRL+Key_N );
        fileMenu->insertItem( "Open", myView, SLOT(open()),    CTRL+Key_O );
        mainMenu->insertItem( "File", fileMenu );

In the example above, pressing CTRL+N or selecting "open" from the menu activates the myView->open() function.

Note that accelerators only work for QPopupMenu items that live in a menu bar. For stand-alone popup menus, use an independent QAccel object.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel(), connectItem(), QAccel and qkeycode.h.

### int QMenuData::insertItem ( const char * text, int id=-1, int index=-1 )

Inserts a menu item with a text. Returns the menu item identifier.

The menu item is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is set equal to the menu item's real index (see below). If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated.

The index specifies the position in the menu. The menu item is appended at the end of the list if index is negative.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. `(QObject*)0`.

See also: removeItem(), changeItem(), setAccel() and connectItem().

### void QMenuData::insertSeparator ( int index=-1 )

Inserts a separator at position index. The separator becomes the last menu item if index is negative.

In a popup menu, a separator is rendered as a horizontal line. In a Motif menubar, a separator is spacing, so the rest of the items (just "Help", normally) are drawn right-justified. In a Windows menubar, all separator are ignored (to comply with the Windows style guide).

Examples: grapher/grapher.cpp application/application.cpp menu/menu.cpp progress/progress.cpp widgets/widgets.cpp

### bool QMenuData::isItemChecked ( int id ) const

Returns TRUE if the menu item has been checked, otherwise FALSE.

See also: setItemChecked().

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

Returns TRUE if the item with identifier id is enabled or FALSE if it is disabled.

See also: setItemEnabled().

### void QMenuData::menuContentsChanged () `[virtual protected]`

Virtual function; notifies subclasses that one or more items have been inserted or removed.

Reimplemented in QMenuBar.

### void QMenuData::menuDelPopup ( QPopupMenu * ) `[virtual protected]`

Virtual function; notifies subclasses that a popup menu item has been removed.

### void QMenuData::menuInsPopup ( QPopupMenu * ) `[virtual protected]`

Virtual function; notifies subclasses that a popup menu item has been inserted.

### void QMenuData::menuStateChanged () `[virtual protected]`

Virtual function; notifies subclasses that one or more items have changed state (enabled/disabled or checked/unchecked).

Reimplemented in QMenuBar.

### QPixmap * QMenuData::pixmap ( int id ) const

Returns the pixmap that has been set for menu item id, or 0 if no pixmap has been set.

See also: changeItem() and text().

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

Removes the menu item which has the identifier id.

See also: removeItemAt() and clear().

### void QMenuData::removeItemAt ( int index )

Removes the menu item at position index.

See also: removeItem() and clear().

### void QMenuData::setAccel ( int key, int id )

Defines an accelerator key for the menu item id.

An accelerator key consists of a key code and a combination of the modifiers `SHIFT, CTRL` and `ALT` (OR'ed or added). The header file qkeycode.h contains a list of key codes.

Defining an accelerator key generates a text which is added to the menu item, for instance, `CTRL` + `Key_O` generates "Ctrl+O". The text is formatted differently for different platforms.

Note that accelerators only work for QPopupMenu items that live in a menu bar. For stand-alone popup menus, use an independent QAccel object.

Example:

        QMenuBar   *mainMenu = new QMenuBar;
        QPopupMenu *fileMenu = new QPopupMenu;      // file sub menu
        fileMenu->insertItem( "Open Document", 67 );// add "Open" item
        fileMenu->setAccel( CTRL + Key_O, 67 );
        fileMenu->insertItem( "Quit", 69 );         // add "Quit" item
        fileMenu->setAccel( CTRL + ALT + Key_Delete, 69 );
        mainMenu->insertItem( "File", fileMenu );   // add the file menu

You can also specify the accelerator in the insertItem() function.

See also: accel(), insertItem(), QAccel and qkeycode.h.

### void QMenuData::setId ( int index, int id )

Sets the menu identifier of the item at index to id.

If index is out of range the operation is ignored.

See also: idAt().

### void QMenuData::setItemChecked ( int id, bool check )

Checks the menu item with id id if check is TRUE, or unchecks it if check is FALSE, and calls setCheckable( TRUE ) if necessary.

See also: isItemChecked().

Examples: grapher/grapher.cpp progress/progress.cpp

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

Enables the menu item with identifier id if enable is TRUE, or disables the item if enable is FALSE.

See also: isItemEnabled().

Examples: menu/menu.cpp progress/progress.cpp widgets/widgets.cpp

### const char * QMenuData::text ( int id ) const

Returns the text that has been set for menu item id, or 0 if no text has been set.

See also: changeItem() and pixmap().

### void QMenuData::updateItem ( int ) `[virtual]`

Virtual function; notifies subclasses about an item that has been changed.

Reimplemented in QMenuBar.

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qmenudata.h: 1998/08/24
- qmenudata.cpp: 1998/11/24