Qt 4.2: QMenuBar Class Reference

|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QMenuBar Class Reference
[QtGui module]

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

     #include <QMenuBar>

Inherits QWidget.

- List of all members, including inherited members
- Qt 3 support members

### Properties

- defaultUp : bool

- 55 properties inherited from QWidget
- 1 property inherited from QObject

### Public Functions

- QMenuBar ( QWidget * parent = 0 )
- ~QMenuBar ()
- QAction * activeAction () const
- QAction * addAction ( const QString & text )
- QAction * addAction ( const QString & text, const QObject * receiver, const char * member )
- QAction * addMenu ( QMenu * menu )
- QMenu * addMenu ( const QString & title )
- QMenu * addMenu ( const QIcon & icon, const QString & title )
- QAction * addSeparator ()
- void clear ()
- QAction * insertMenu ( QAction * before, QMenu * menu )
- QAction * insertSeparator ( QAction * before )
- bool isDefaultUp () const
- void setActiveAction ( QAction * act )
- void setDefaultUp ( bool )

- 195 public functions inherited from QWidget
- 29 public functions inherited from QObject
- 12 public functions inherited from QPaintDevice

### Signals

- void hovered ( QAction * action )
- void triggered ( QAction * action )

- 1 signal inherited from QWidget
- 1 signal inherited from QObject

### Additional Inherited Members

- 19 public slots inherited from QWidget
- 1 public slot inherited from QObject
- 4 static public members inherited from QWidget
- 5 static public members inherited from QObject
- 38 protected functions inherited from QWidget
- 7 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice
- 1 protected slot inherited from QWidget

## Detailed Description

The QMenuBar class provides a horizontal menu bar.

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

     menubar->addMenu(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.)

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.

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

Example (from the Menus example):

         fileMenu = menuBar()->addMenu(tr("&File"));
         fileMenu->addAction(newAct);

Menu items may be removed with removeAction().

### Platform Dependent Look and Feel

Different platforms have different requirements for the appearance of menu bars and their behavior when the user interacts with them. For example, Windows systems are often configured so that the underlined character mnemonics that indicate keyboard shortcuts for items in the menu bar are only shown when the Alt key is pressed.

| The Plastique widget style, like most other styles, handles the Help menu in the same way as it handles any other menu.
| The Motif widget style treats Help menus in a special way, placing them at right-hand end of the menu bar.

### QMenuBar on Qt/Mac

QMenuBar on Qt/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 Qt::Window) will be used for the system-wide menubar.

Qt/Mac also provides a menubar merging feature to make QMenuBar conform more closely to accepted Mac OS X menubar layout. The merging functionality is based on string matching the title of a QMenu entry. These strings are translated (using QObject::tr()) in the "QMenuBar" 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 <application name> | 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 <application name> | If this entry is not found a default Quit item will be created to call QApplication::quit()

You can override this behavior by using the QAction::menuRole() property.

Note: The text used for the application name in the menu bar is obtained from the value set in the `Info.plist` file in the application's bundle. See Deploying an Application on Qt/Mac for more information.

### Examples

The Menus example shows how to use QMenuBar and QMenu. The other main window application examples also provide menus using these classes.

See also QMenu, QShortcut, QAction, Introduction to Apple Human Interface Guidelines, GUI Design Handbook: Menu Bar, and Menus Example.

## Property Documentation

### defaultUp : bool

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.

Access functions:

- bool isDefaultUp () const
- void setDefaultUp ( bool )

## Member Function Documentation

### QMenuBar::QMenuBar ( QWidget * parent = 0 )

Constructs a menu bar with parent parent.

### QMenuBar::~QMenuBar ()

Destroys the menu bar.

### QAction * QMenuBar::activeAction () const

Returns the QAction that is currently highlighted. A null pointer will be returned if no action is currently selected.

See also setActiveAction().

### QAction * QMenuBar::addAction ( const QString & text )

This convenience function creates a new action with text. The function adds the newly created action to the menu's list of actions, and returns it.

See also QWidget::addAction().

### QAction * QMenuBar::addAction ( const QString & text, const QObject * receiver, const char * member )

This is an overloaded member function, provided for convenience.

This convenience function creates a new action with the given text. The action's triggered() signal is connected to the receiver's member slot. The function adds the newly created action to the menu's list of actions and returns it.

See also QWidget::addAction().

### QAction * QMenuBar::addMenu ( QMenu * menu )

Appends menu to the menubar. Returns the menu's menuAction().

See also QWidget::addAction() and QMenu::menuAction().

### QMenu * QMenuBar::addMenu ( const QString & title )

This is an overloaded member function, provided for convenience.

Appends a new QMenu with title to the menubar. The menubar takes ownership of the menu. Returns the new menu.

See also QWidget::addAction() and QMenu::menuAction().

### QMenu * QMenuBar::addMenu ( const QIcon & icon, const QString & title )

This is an overloaded member function, provided for convenience.

Appends a new QMenu with icon and title to the menubar. The menubar takes ownership of the menu. Returns the new menu.

See also QWidget::addAction() and QMenu::menuAction().

### QAction * QMenuBar::addSeparator ()

Appends a separator to the menu.

### void QMenuBar::clear ()

Removes all the actions from the menu bar.

See also removeAction().

### void QMenuBar::hovered ( QAction * action ) `[signal]`

This signal is emitted when a menu action is highlighted; action is the action that caused the event to be sent.

Often this is used to update status information.

See also triggered() and QAction::hovered().

### QAction * QMenuBar::insertMenu ( QAction * before, QMenu * menu )

This convenience function inserts menu before action before and returns the menus menuAction().

See also QWidget::insertAction() and addMenu().

### QAction * QMenuBar::insertSeparator ( QAction * before )

This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true. The function inserts the newly created action into this menubar's list of actions before action before and returns it.

See also QWidget::insertAction() and addSeparator().

### void QMenuBar::setActiveAction ( QAction * act )

Sets the currently highlighted action to act.

This function was introduced in Qt 4.1.

See also activeAction().

### void QMenuBar::triggered ( QAction * action ) `[signal]`

This signal is emitted when a menu action is selected; action is the action that caused the event to be sent.

Normally, you connect each menu action to a single slot using QAction::triggered(), 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 hovered() and QAction::triggered().

## Member Type Documentation

### enum QMenuBar::Separator

Constant | Value
--- | ---
`QMenuBar::Never` | `0`
`QMenuBar::InWindowsStyle` | `1`

## Member Function Documentation

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

Use one of the constructors that doesn't take the name argument and then use setObjectName() instead.

### QKeySequence QMenuBar::accel ( int id ) const

Use shortcut() on the relevant QAction instead.

See also setAccel().

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

Use activate() on the relevant QAction instead.

### void QMenuBar::activated ( int itemId ) `[signal]`

Use triggered() instead.

### bool QMenuBar::autoGeometry () const

Returns true if the menu bar automatically resizes itself when its parent widget is resized; otherwise returns false.

This feature is provided to help porting to Qt 4. We recommend against using it in new code.

See also setAutoGeometry().

### void QMenuBar::changeItem ( int id, const QString & text )

Use setText() on the relevant QAction instead.

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

This is an overloaded member function, provided for convenience.

Use setText() on the relevant QAction instead.

### void QMenuBar::changeItem ( int id, const QIcon & icon, const QString & text )

This is an overloaded member function, provided for convenience.

Use setIcon() and setText() on the relevant QAction instead.

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

Use connect() on the relevant QAction instead.

### uint QMenuBar::count () const

Use actions().count() instead.

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

Use disconnect() on the relevant QAction instead.

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

Use actions instead.

### int QMenuBar::frameWidth () const

Use style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, this) instead.

### void QMenuBar::highlighted ( int itemId ) `[signal]`

Use hovered() instead.

### QIcon QMenuBar::iconSet ( int id ) const

Use icon() on the relevant QAction instead.

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

Use actions instead.

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

Use actions().indexOf(action) on the relevant QAction instead.

### int QMenuBar::insertItem ( const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0, int id = -1, int index = -1 )

Use one of the insertAction() or addAction() overloads instead.

### int QMenuBar::insertItem ( const QIcon & icon, const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use one of the insertAction() or addAction() overloads instead.

### int QMenuBar::insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use one of the insertAction(), addAction(), insertMenu(), or addMenu() overloads instead.

### int QMenuBar::insertItem ( const QString & text, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use one of the insertAction() or addAction() overloads instead.

### int QMenuBar::insertItem ( const QIcon & icon, const QString & text, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use one of the insertAction(), addAction(), insertMenu(), or addMenu() overloads instead.

### int QMenuBar::insertItem ( const QString & text, QMenu * popup, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use one of the insertMenu(), or addMenu() overloads instead.

### int QMenuBar::insertItem ( const QIcon & icon, const QString & text, QMenu * popup, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use one of the insertMenu(), or addMenu() overloads instead.

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

This is an overloaded member function, provided for convenience.

Use one of the insertAction(), addAction(), insertMenu(), or addMenu() overloads instead.

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

This is an overloaded member function, provided for convenience.

Use one of the insertMenu(), or addMenu() overloads instead.

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

This is an overloaded member function, provided for convenience.

Use addSeparator() or insertAction() instead.

For example, if you have code like

     menuBar->insertSeparator();

you can rewrite it as

     menuBar->addSeparator();

This function was introduced in Qt 4.2.

### bool QMenuBar::isItemActive ( int id ) const

Use activeAction() instead.

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

Use isChecked() on the relevant QAction instead.

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

Use isEnabled() on the relevant QAction instead.

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

Use isVisible() on the relevant QAction instead.

### int QMenuBar::itemAtPos ( const QPoint & p ) `[protected]`

There is no equivalent way to achieve this in Qt 4.

### int QMenuBar::itemParameter ( int id ) const

Use QAction::data() instead.

See also setItemParameter().

### QRect QMenuBar::itemRect ( int index ) `[protected]`

Use actionGeometry() on the relevant QAction instead.

### int QMenuBar::margin () const

Returns the with of the the margin around the contents of the widget.

Use QWidget::getContentsMargins() instead.

See also setMargin() and QWidget::getContentsMargins().

### QPixmap QMenuBar::pixmap ( int id ) const

Use QPixmap(icon()) on the relevant QAction instead.

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

Use removeAction() instead.

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

Use removeAction() instead.

### Separator QMenuBar::separator () const

This function is provided only to make old code compile.

See also setSeparator().

### void QMenuBar::setAccel ( const QKeySequence & key, int id )

Use setShortcut() on the relevant QAction instead.

See also accel().

### void QMenuBar::setAutoGeometry ( bool b )

Sets whether the menu bar should automatically resize itself when its parent widget is resized.

This feature is provided to help porting to Qt 4. We recommend against using it in new code.

See also autoGeometry().

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

Use setChecked() on the relevant QAction instead.

See also isItemChecked().

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

Use setEnabled() on the relevant QAction instead.

See also isItemEnabled().

### bool QMenuBar::setItemParameter ( int id, int param )

Use QAction::setData() instead.

See also itemParameter().

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

Use setVisible() on the relevant QAction instead.

See also isItemVisible().

### void QMenuBar::setMargin ( int margin )

Sets the width of the margin around the contents of the widget to margin.

Use QWidget::setContentsMargins() instead.

See also margin() and QWidget::setContentsMargins().

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

This function is provided only to make old code compile.

See also separator().

### void QMenuBar::setWhatsThis ( int id, const QString & w )

Use setWhatsThis() on the relevant QAction instead.

See also whatsThis().

### QString QMenuBar::text ( int id ) const

Use text() on the relevant QAction instead.

### QString QMenuBar::whatsThis ( int id ) const

Use whatsThis() on the relevant QAction instead.

See also setWhatsThis().