TQMenuBar Class

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

# TQMenuBar Class Reference

The TQMenuBar class provides a horizontal menu bar. More...

`#include <ntqmenubar.h>`

Inherits TQFrame and TQMenuData.

List of all member functions.

## Public Members

- TQMenuBar ( TQWidget * parent = 0, const char * name = 0 )
- ~TQMenuBar ()
- virtual void show ()
- virtual void hide ()
- virtual int heightForWidth ( int max_width ) const
- enum Separator { Never = 0, InWindowsStyle = 1 }
- Separator separator () const (obsolete)
- virtual void setSeparator ( Separator when ) (obsolete)
- void setDefaultUp ( bool )
- bool isDefaultUp () const

## Signals

- void activated ( int id )
- void highlighted ( int id )

## Important Inherited Members

- int insertItem ( const TQString & text, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )
- int insertItem ( const TQIconSet & icon, const TQString & text, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )
- int insertItem ( const TQPixmap & pixmap, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )
- int insertItem ( const TQIconSet & icon, const TQPixmap & pixmap, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )
- int insertItem ( const TQString & text, int id = -1, int index = -1 )
- int insertItem ( const TQIconSet & icon, const TQString & text, int id = -1, int index = -1 )
- int insertItem ( const TQString & text, TQPopupMenu * popup, int id = -1, int index = -1 )
- int insertItem ( const TQIconSet & icon, const TQString & text, TQPopupMenu * popup, int id = -1, int index = -1 )
- int insertItem ( const TQPixmap & pixmap, int id = -1, int index = -1 )
- int insertItem ( const TQIconSet & icon, const TQPixmap & pixmap, int id = -1, int index = -1 )
- int insertItem ( const TQPixmap & pixmap, TQPopupMenu * popup, int id = -1, int index = -1 )
- int insertItem ( const TQIconSet & icon, const TQPixmap & pixmap, TQPopupMenu * popup, int id = -1, int index = -1 )
- int insertItem ( TQWidget * widget, int id = -1, int index = -1 )
- int insertItem ( const TQIconSet & icon, TQCustomMenuItem * custom, int id = -1, int index = -1 )
- int insertItem ( TQCustomMenuItem * custom, int id = -1, int index = -1 )
- int insertSeparator ( int index = -1 )
- void removeItem ( int id )
- void clear ()
- bool isItemEnabled ( int id ) const
- void setItemEnabled ( int id, bool enable )
- bool isItemVisible ( int id ) const
- void setItemVisible ( int id, bool visible )

## Properties

- bool defaultUp - the popup orientation
- Separator separator - in which cases a menubar sparator is drawn (obsolete)

## Protected Members

- virtual void drawContents ( TQPainter * p )
- virtual void menuContentsChanged ()
- virtual void menuStateChanged ()

## Detailed Description

The TQMenuBar class provides a horizontal menu bar.

A menu bar consists of a list of pull-down menu items. You add menu items with insertItem(). For example, asuming that `menubar` is a pointer to a TQMenuBar and `filemenu` is a pointer to a TQPopupMenu, the following statement inserts the menu into the menu bar:

        menubar->insertItem( "&File", filemenu );

The ampersand in the menu item's text sets Alt+F as a shortcut for this menu. (You can use "&&" to get a real ampersand in the menu bar.)

Items are either enabled or disabled. You toggle their state with setItemEnabled().

There is no need to lay out a menu bar. It automatically sets its own geometry to the top of the parent widget and changes it appropriately whenever the parent is resized.

Example of creating a menu bar with menu items (from menu/menu.cpp):

            TQPopupMenu *file = new TQPopupMenu( this );

            file->insertItem( p1, "&Open",  this, TQ_SLOT(open()), CTRL+Key_O );
            file->insertItem( p2, "&New", this, TQ_SLOT(news()), CTRL+Key_N );

            menu = new TQMenuBar( this );

            menu->insertItem( "&File", file );

In most main window style applications you would use the menuBar() provided in TQMainWindow, adding TQPopupMenus to the menu bar and adding TQActions to the popup menus.

Example (from action/application.cpp):

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

Menu items can have text and pixmaps (or iconsets), see the various insertItem() overloads, as well as separators, see insertSeparator(). You can also add custom menu items that are derived from TQCustomMenuItem.

Menu items may be removed with removeItem() and enabled or disabled with setItemEnabled().

### TQMenuBar on TQt/Mac

TQMenuBar on TQt/Mac is a wrapper for using the system-wide menubar. If you have multiple menubars in one dialog the outermost menubar (normally inside a widget with widget flag WType_TopLevel) will be used for the system-wide menubar.

Note that arbitrary TQt widgets cannot be inserted into a TQMenuBar on the Mac because TQt uses Mac's native menus which don't support this functionality. This limitation does not apply to stand-alone TQPopupMenus.

TQt/Mac also provides a menubar merging feature to make TQMenuBar conform more closely to accepted Mac OS X menubar layout. The merging functionality is based on string matching the title of a TQPopupMenu entry. These strings are translated (using TQObject::tr()) in the "TQMenuBar" context. If an entry is moved its slots will still fire as if it was in the original place. The table below outlines the strings looked for and where the entry is placed if matched:

String matches | Placement | Notes
--- | --- | ---
about.* | Application Menu | About | If this entry is not found no About item will appear in the Application Menu
config, options, setup, settings or preferences | Application Menu | Preferences | If this entry is not found the Settings item will be disabled
quit or exit | Application Menu | Quit | If this entry is not found a default Quit item will be created to call TQApplication::quit()

menu/menu.cpp is an example of TQMenuBar and TQPopupMenu use.

See also TQPopupMenu, TQAccel, TQAction, Aqua Style Guidelines, GUI Design Handbook: Menu Bar, and Main Window and Related Classes.

## Member Type Documentation

### TQMenuBar::Separator

This enum type is used to decide whether TQMenuBar should draw a separator line at its bottom.

- `TQMenuBar::Never` - In many applications there is already a separator, and having two looks wrong.
- `TQMenuBar::InWindowsStyle` - In some other applications a separator looks good in Windows style, but nowhere else.

## Member Function Documentation

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

Constructs a menu bar called name with parent parent.

### TQMenuBar::~TQMenuBar ()

Destroys the menu bar.

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

This signal is emitted when a menu item is selected; id is the id of the selected item.

Normally you will connect each menu item to a single slot using TQMenuData::insertItem(), but sometimes you will want to connect several items to a single slot (most often if the user selects from an array). This signal is useful in such cases.

See also highlighted() and TQMenuData::insertItem().

Example: progress/progress.cpp.

### void TQMenuData::clear ()

Removes all menu items.

See also removeItem() and removeItemAt().

Examples: mdi/application.cpp and qwerty/qwerty.cpp.

### void TQMenuBar::drawContents ( TQPainter * p )`[virtual protected]`

Called from TQFrame::paintEvent(). Draws the menu bar contents using painter p.

Reimplemented from TQFrame.

### int TQMenuBar::heightForWidth ( int max_width ) const`[virtual]`

Returns the height that the menu would resize itself to if its parent (and hence itself) resized to the given max_width. This can be useful for simple layout tasks in which the height of the menu bar is needed after items have been inserted. See showimg/showimg.cpp for an example of the usage.

Example: showimg/showimg.cpp.

Reimplemented from TQWidget.

### void TQMenuBar::hide ()`[virtual]`

Reimplements TQWidget::hide() in order to deselect any selected item, and calls setUpLayout() for the main window.

Example: grapher/grapher.cpp.

Reimplemented from TQWidget.

### void TQMenuBar::highlighted ( int id )`[signal]`

This signal is emitted when a menu item is highlighted; id is the id of the highlighted item.

Normally, you will connect each menu item to a single slot using TQMenuData::insertItem(), but sometimes you will want to connect several items to a single slot (most often if the user selects from an array). This signal is useful in such cases.

See also activated() and TQMenuData::insertItem().

### int TQMenuData::insertItem ( const TQString & text, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )

The family of insertItem() functions inserts menu items into a popup menu or a menu bar.

A menu item is usually either a text string or a pixmap, both with an optional icon or keyboard accelerator. For special cases it is also possible to insert custom items (see TQCustomMenuItem) or even widgets into popup menus.

Some insertItem() members take a popup menu as an additional argument. Use this to insert submenus into existing menus or pulldown menus into a menu bar.

The number of insert functions may look confusing, but they are actually quite simple to use.

This default version inserts a menu item with the text text, the accelerator key accel, an id and an optional index and connects it to the slot member in the object receiver.

Example:

            TQMenuBar   *mainMenu = new TQMenuBar;
            TQPopupMenu *fileMenu = new TQPopupMenu;
            fileMenu->insertItem( "New",  myView, TQ_SLOT(newFile()), CTRL+Key_N );
            fileMenu->insertItem( "Open", myView, TQ_SLOT(open()),    CTRL+Key_O );
            mainMenu->insertItem( "File", fileMenu );

Not all insert functions take an object/slot parameter or an accelerator key. Use connectItem() and setAccel() on those items.

If you need to translate accelerators, use tr() with the text and accelerator. (For translations use a string key sequence.):

            fileMenu->insertItem( tr("Open"), myView, TQ_SLOT(open()),
                                  tr("Ctrl+O") );

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

Some insert functions take a TQIconSet parameter to specify the little menu item icon. Note that you can always pass a TQPixmap object instead.

The id specifies the identification number associated with the menu item. Note that only positive values are valid, as a negative value will make TQt select a unique id for the item.

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 keyboard accelerators in TQt are not application-global, instead they are bound to a certain top-level window. For example, accelerators in TQPopupMenu items only work for menus that are associated with a certain window. This is true for popup menus that live in a menu bar since their accelerators will then be installed in the menu bar itself. This also applies to stand-alone popup menus that have a top-level widget in their parentWidget() chain. The menu will then install its accelerator object on that top-level widget. For all other cases use an independent TQAccel object.

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

Warning: On Mac OS X, items that connect to a slot that are inserted into a menubar will not function as we use the native menubar that knows nothing about signals or slots. Instead insert the items into a popup menu and insert the popup menu into the menubar. This may be fixed in a future TQt version.

Returns the allocated menu identifier number (id if id >= 0).

See also removeItem(), changeItem(), setAccel(), connectItem(), TQAccel, and ntqnamespace.h.

Examples: addressbook/mainwindow.cpp, canvas/canvas.cpp, menu/menu.cpp, qwerty/qwerty.cpp, scrollview/scrollview.cpp, showimg/showimg.cpp, and sound/sound.cpp.

### int TQMenuData::insertItem ( const TQIconSet & icon, const TQString & text, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )

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

Inserts a menu item with icon icon, text text, accelerator accel, optional id id, and optional index position. The menu item is connected it to the receiver's member slot. The icon will be displayed to the left of the text in the item.

Returns the allocated menu identifier number (id if id >= 0).

See also removeItem(), changeItem(), setAccel(), connectItem(), TQAccel, and ntqnamespace.h.

### int TQMenuData::insertItem ( const TQPixmap & pixmap, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )

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

Inserts a menu item with pixmap pixmap, accelerator accel, optional id id, and optional index position. The menu item is connected it to the receiver's member slot. The icon will be displayed to the left of the text in the item.

To look best when being highlighted as a menu item, the pixmap should provide a mask (see TQPixmap::mask()).

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQIconSet & icon, const TQPixmap & pixmap, const TQObject * receiver, const char * member, const TQKeySequence & accel = 0, int id = -1, int index = -1 )

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

Inserts a menu item with icon icon, pixmap pixmap, accelerator accel, optional id id, and optional index position. The icon will be displayed to the left of the pixmap in the item. The item is connected to the member slot in the receiver object.

To look best when being highlighted as a menu item, the pixmap should provide a mask (see TQPixmap::mask()).

Returns the allocated menu identifier number (id if id >= 0).

See also removeItem(), changeItem(), setAccel(), connectItem(), TQAccel, and ntqnamespace.h.

### int TQMenuData::insertItem ( const TQString & text, int id = -1, int index = -1 )

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

Inserts a menu item with text text, optional id id, and optional index position.

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQIconSet & icon, const TQString & text, int id = -1, int index = -1 )

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

Inserts a menu item with icon icon, text text, optional id id, and optional index position. The icon will be displayed to the left of the text in the item.

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQString & text, TQPopupMenu * popup, int id = -1, int index = -1 )

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

Inserts a menu item with text text, submenu popup, optional id id, and optional index position.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQIconSet & icon, const TQString & text, TQPopupMenu * popup, int id = -1, int index = -1 )

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

Inserts a menu item with icon icon, text text, submenu popup, optional id id, and optional index position. The icon will be displayed to the left of the text in the item.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQPixmap & pixmap, int id = -1, int index = -1 )

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

Inserts a menu item with pixmap pixmap, optional id id, and optional index position.

To look best when being highlighted as a menu item, the pixmap should provide a mask (see TQPixmap::mask()).

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQIconSet & icon, const TQPixmap & pixmap, int id = -1, int index = -1 )

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

Inserts a menu item with icon icon, pixmap pixmap, optional id id, and optional index position. The icon will be displayed to the left of the pixmap in the item.

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQPixmap & pixmap, TQPopupMenu * popup, int id = -1, int index = -1 )

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

Inserts a menu item with pixmap pixmap, submenu popup, optional id id, and optional index position.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( const TQIconSet & icon, const TQPixmap & pixmap, TQPopupMenu * popup, int id = -1, int index = -1 )

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

Inserts a menu item with icon icon, pixmap pixmap submenu popup, optional id id, and optional index position. The icon will be displayed to the left of the pixmap in the item.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the allocated menu identifier number (id if id >= 0).

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

### int TQMenuData::insertItem ( TQWidget * widget, int id = -1, int index = -1 )

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

Inserts a menu item that consists of the widget widget with optional id id, and optional index position.

Ownership of widget is transferred to the popup menu or to the menu bar.

Theoretically, any widget can be inserted into a popup menu. In practice, this only makes sense with certain widgets.

If a widget is not focus-enabled (see TQWidget::isFocusEnabled()), the menu treats it as a separator; this means that the item is not selectable and will never get focus. In this way you can, for example, simply insert a TQLabel if you need a popup menu with a title.

If the widget is focus-enabled it will get focus when the user traverses the popup menu with the arrow keys. If the widget does not accept `ArrowUp` and `ArrowDown` in its key event handler, the focus will move back to the menu when the respective arrow key is hit one more time. This works with a TQLineEdit, for example. If the widget accepts the arrow key itself, it must also provide the possibility to put the focus back on the menu again by calling TQWidget::focusNextPrevChild(). Futhermore, if the embedded widget closes the menu when the user made a selection, this can be done safely by calling:

            if ( isVisible() &&
                 parentWidget() &&
                 parentWidget()->inherits("TQPopupMenu") )
                parentWidget()->close();

Returns the allocated menu identifier number (id if id >= 0).

See also removeItem().

### int TQMenuData::insertItem ( const TQIconSet & icon, TQCustomMenuItem * custom, int id = -1, int index = -1 )

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

Inserts a custom menu item custom with an icon and with optional id id, and optional index position.

This only works with popup menus. It is not supported for menu bars. Ownership of custom is transferred to the popup menu.

If you want to connect a custom item to a slot, use connectItem().

Returns the allocated menu identifier number (id if id >= 0).

See also connectItem(), removeItem(), and TQCustomMenuItem.

### int TQMenuData::insertItem ( TQCustomMenuItem * custom, int id = -1, int index = -1 )

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

Inserts a custom menu item custom with optional id id, and optional index position.

This only works with popup menus. It is not supported for menu bars. Ownership of custom is transferred to the popup menu.

If you want to connect a custom item to a slot, use connectItem().

Returns the allocated menu identifier number (id if id >= 0).

See also connectItem(), removeItem(), and TQCustomMenuItem.

### int TQMenuData::insertSeparator ( int index = -1 )

Inserts a separator at position index, and returns the menu identifier number allocated to it. 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 menu bar a separator is spacing, so the rest of the items (normally just "Help") are drawn right-justified. In a Windows menu bar separators are ignored (to comply with the Windows style guidelines).

Examples: addressbook/mainwindow.cpp, menu/menu.cpp, progress/progress.cpp, qwerty/qwerty.cpp, scrollview/scrollview.cpp, showimg/showimg.cpp, and sound/sound.cpp.

### bool TQMenuBar::isDefaultUp () const

Returns the popup orientation. See the "defaultUp" property for details.

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

Returns TRUE if the item with identifier id is enabled; otherwise returns FALSE

See also setItemEnabled() and isItemVisible().

### bool TQMenuData::isItemVisible ( int id ) const

Returns TRUE if the menu item with the id id is visible; otherwise returns FALSE.

See also setItemVisible().

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

Recomputes the menu bar's display data according to the new contents.

You should never need to call this; it is called automatically by TQMenuData whenever it needs to be called.

Reimplemented from TQMenuData.

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

Recomputes the menu bar's display data according to the new state.

You should never need to call this; it is called automatically by TQMenuData whenever it needs to be called.

Reimplemented from TQMenuData.

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

Removes the menu item that has the identifier id.

See also removeItemAt() and clear().

Example: chart/chartform.cpp.

### Separator TQMenuBar::separator () const

Returns in which cases a menubar sparator is drawn. See the "separator" property for details.

### void TQMenuBar::setDefaultUp ( bool )

Sets the popup orientation. See the "defaultUp" property for details.

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

If enable is TRUE, enables the menu item with identifier id; otherwise disables the menu item with identifier id.

See also isItemEnabled().

Examples: mdi/application.cpp, menu/menu.cpp, progress/progress.cpp, and showimg/showimg.cpp.

### void TQMenuData::setItemVisible ( int id, bool visible )

If visible is TRUE, shows the menu item with id id; otherwise hides the menu item with id id.

See also isItemVisible() and isItemEnabled().

### void TQMenuBar::setSeparator ( Separator when )`[virtual]`

Sets in which cases a menubar sparator is drawn to when. See the "separator" property for details.

### void TQMenuBar::show ()`[virtual]`

Reimplements TQWidget::show() in order to set up the correct keyboard accelerators and to raise itself to the top of the widget stack.

Example: grapher/grapher.cpp.

Reimplemented from TQWidget.

## Property Documentation

### bool defaultUp

This property holds the popup orientation.

The default popup orientation. By default, menus pop "down" the screen. By setting the property to TRUE, the menu will pop "up". You might call this for menus that are below the document to which they refer.

If the menu would not fit on the screen, the other direction is used automatically.

Set this property's value with setDefaultUp() and get this property's value with isDefaultUp().

### Separator separator

This property holds in which cases a menubar sparator is drawn.

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

Set this property's value with setSeparator() and get this property's value with separator().

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