Qt Toolkit - QMenuBar Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QMenuBar Class Reference

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

`#include <qmenubar.h>`

Inherits QFrame and QMenuData.

List of all member functions.

## Public Members

- QMenuBar ( QWidget * parent=0, const char * name=0 )
- ~QMenuBar ()
- virtual void updateItem ( int id ) (internal)
- virtual void show ()
- virtual void hide ()
- virtual bool eventFilter ( QObject *, QEvent * )
- virtual int heightForWidth ( int ) const
- enum Separator { Never=0, InWindowsStyle=1 }
- Separator separator () const
- virtual void setSeparator ( Separator when )
- void setDefaultUp ( bool )
- bool isDefaultUp () const

## Signals

- void activated ( int itemId )
- void highlighted ( int itemId )

## Important Inherited Members

- int insertItem ( QCustomMenuItem * custom, int id, int index )
- int insertItem ( QWidget * widget, int id, int index )
- int insertItem ( const QIconSet & icon, QCustomMenuItem * custom, int id, int index )
- int insertItem ( const QIconSet & icon, const QPixmap & pixmap, QPopupMenu * popup, int id, int index )
- int insertItem ( const QIconSet & icon, const QPixmap & pixmap, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QIconSet & icon, const QPixmap & pixmap, int id, int index )
- int insertItem ( const QIconSet & icon, const QString & text, QPopupMenu * popup, int id, int index )
- int insertItem ( const QIconSet & icon, const QString & text, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QIconSet & icon, const QString & text, int id, int index )
- int insertItem ( const QPixmap & pixmap, QPopupMenu * popup, int id, int index )
- int insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QPixmap & pixmap, int id, int index )
- int insertItem ( const QString & text, QPopupMenu * popup, int id, int index )
- int insertItem ( const QString & text, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QString & text, int id, int index )
- void removeItem ( int id )
- void clear ()
- int insertSeparator ( int index )
- void setItemEnabled ( int id, bool enable )
- bool isItemEnabled ( int id ) const

## Protected Members

- virtual void drawContents ( QPainter * )
- virtual void fontChange ( const QFont & ) (internal)
- virtual void menuContentsChanged ()
- virtual void menuStateChanged ()

## Properties

Type | Name | READ | WRITE | Options
--- | --- | --- | --- | ---
Separator | separator | separator | setSeparator |
bool | defaultUp | isDefaultUp | setDefaultUp |

## Detailed Description

The QMenuBar class provides a horizontal menu bar.

A menu bar consists of a list of submenu items, so-called pulldown menus. You add submenu items with insertItem(). Assuming that `menubar` is a pointer to a QMenuBar and `filemenu` a pointer to a QPopupMenu,

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

inserts the menu into the menu bar. The ampersand in the item text declares Alt-f as shortcut for this menu. Use "&&" to get a real ampsend in the menubar.

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

Note that there is no need to layout 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.

menu/menu.cpp is a typical example of QMenuBar and QPopupMenu use.

See also QPopupMenu and GUI Design Handbook: Menu Bar

Examples: grapher/grapher.cpp layout/layout.cpp menu/menu.cpp progress/progress.cpp scrollview/scrollview.cpp

## Member Type Documentation

### QMenuBar::Separator

This enum type is used to decide whether QMenuBar should draw a separator line at its bottom. The possible values are:

- `Never` - in many applications, there already is a separator, and two looks stupid.
- `InWindowsStyle` - in some other applications, a separator looks good in Windows style but not else.

## Member Function Documentation

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

Constructs a menu bar with a parent and a name.

### QMenuBar::~QMenuBar ()

Destroys the menu bar.

### void QMenuBar::activateItemAt ( int index )

Reimplemented for internal reasons; the API is not affected.

### void QMenuBar::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 QMenuData::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 handy in such cases.

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

### bool QMenuBar::customWhatsThis () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::drawContents ( QPainter * p ) `[virtual protected]`

Called from QFrame::paintEvent().

Reimplemented from QFrame.

### bool QMenuBar::eventFilter ( QObject * object, QEvent * event ) `[virtual]`

This function is used to adjust the menu bar's geometry to the parent widget's. Note that this is not part of the public interface - the function is `public` only because QObject::eventFilter() is.

Reimplemented from QObject.

### void QMenuBar::focusInEvent ( QFocusEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::focusOutEvent ( QFocusEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### int QMenuBar::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 width. This can be useful for simple layout tasks where the height of the menubar is needed after items have been inserted. See examples/showimg/showimg.cpp for an example of the usage.

Reimplemented from QWidget.

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

Reimplements QWidget::hide() in order to deselect any selected item and calls setUpLayout() for the mainwindow.

Examples: grapher/grapher.cpp

Reimplemented from QWidget.

### void QMenuBar::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 QMenuData::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 handy in such cases.

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

### bool QMenuBar::isDefaultUp () const

Returns whether the menus default to popping "down" the screen (the default), or "up".

See also setDefaultUp();.

### void QMenuBar::keyPressEvent ( QKeyEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::leaveEvent ( QEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::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 QMenuData whenever it needs to be called.

Reimplemented from QMenuData.

### void QMenuBar::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 QMenuData whenever it needs to be called.

Reimplemented from QMenuData.

### QSize QMenuBar::minimumSize () const

Reimplemented for internal reasons; the API is not affected.

### QSize QMenuBar::minimumSizeHint () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::mouseMoveEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::mousePressEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::mouseReleaseEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::resizeEvent ( QResizeEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### QMenuBar::Separator QMenuBar::separator() const

Returns the currently set Separator usage.

See also Separator and setSeparator().

### void QMenuBar::setDefaultUp ( bool on )

Sets the default popup orientation. By default, menus pop "down" the screen. By calling setDefaultUp(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 rather than the default.

See also defaultUp();.

### void QMenuBar::setSeparator ( Separator when )

When a menubar is used above an unframed widget, it may look better with a separating line when displayed with WindowsStyle.

This function sets the usage of such a separator to appear either QMenuBar::Never, or QMenuBar::InWindowsStyle.

The default is QMenuBar::Never.

See also Separator and separator().

Examples: layout/layout.cpp scrollview/scrollview.cpp

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

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

Examples: grapher/grapher.cpp

Reimplemented from QWidget.

### QSize QMenuBar::sizeHint () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::styleChange ( QStyle & old ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMenuBar::fontChange ( const QFont & f ) `[virtual protected]`

For internal use only.

Reimplemented from QWidget.

### void QMenuBar::updateItem ( int id ) `[virtual]`

For internal use only.

Reimplemented from QMenuData.

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

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.