|  | Home · All Classes · Modules

# QMenuBar Class Reference
[QtGui module]

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

Inherits QWidget.

### Methods

- __init__ (self, QWidget parent = None)
- QAction actionAt (self, QPoint)
- actionEvent (self, QActionEvent)
- QRect actionGeometry (self, QAction)
- QAction activeAction (self)
- addAction (self, QAction action)
- QAction addAction (self, QString text)
- QAction addAction (self, QString text, QObject receiver, SLOT() member)
- QAction addAction (self, QString text, callable receiver)
- QAction addMenu (self, QMenu menu)
- QMenu addMenu (self, QString title)
- QMenu addMenu (self, QIcon icon, QString title)
- QAction addSeparator (self)
- changeEvent (self, QEvent)
- clear (self)
- bool event (self, QEvent)
- bool eventFilter (self, QObject, QEvent)
- focusInEvent (self, QFocusEvent)
- focusOutEvent (self, QFocusEvent)
- int heightForWidth (self, int)
- initStyleOption (self, QStyleOptionMenuItem option, QAction action)
- QAction insertMenu (self, QAction before, QMenu menu)
- QAction insertSeparator (self, QAction before)
- bool isDefaultUp (self)
- keyPressEvent (self, QKeyEvent)
- leaveEvent (self, QEvent)
- QSize minimumSizeHint (self)
- mouseMoveEvent (self, QMouseEvent)
- mousePressEvent (self, QMouseEvent)
- mouseReleaseEvent (self, QMouseEvent)
- paintEvent (self, QPaintEvent)
- resizeEvent (self, QResizeEvent)
- setActiveAction (self, QAction action)
- setDefaultUp (self, bool)
- setVisible (self, bool visible)
- QSize sizeHint (self)

### Qt Signals

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

## 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 menu bar. If you have multiple menu bars in one dialog the outermost menu bar (normally inside a widget with widget flag Qt.Window) will be used for the system-wide menu bar.

Qt/Mac also provides a menu bar merging feature to make QMenuBar conform more closely to accepted Mac OS X menu bar 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.

If you wish to make all windows in a Mac application share the same menu bar, you need to create a menu bar that does not have a parent. The menu bar is created like this:

       QMenuBar *menuBar = new QMenuBar(0);

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.

## Method Documentation

### QMenuBar.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a menu bar with parent parent.

### QAction QMenuBar.actionAt (self, QPoint)

### QMenuBar.actionEvent (self, QActionEvent)

### QRect QMenuBar.actionGeometry (self, QAction)

### QAction QMenuBar.activeAction (self)

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

See also setActiveAction().

### QMenuBar.addAction (self, QAction action)

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 (self, QString text)

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.addAction (self, QString text, QObject receiver, SLOT() member)

### QAction QMenuBar.addAction (self, QString text, callable receiver)

### QAction QMenuBar.addMenu (self, QMenu menu)

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

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

### QMenu QMenuBar.addMenu (self, QString title)

This is an overloaded member function, provided for convenience.

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

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

### QMenu QMenuBar.addMenu (self, QIcon icon, QString title)

This is an overloaded member function, provided for convenience.

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

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

### QAction QMenuBar.addSeparator (self)

Appends a separator to the menu.

### QMenuBar.changeEvent (self, QEvent)

### QMenuBar.clear (self)

Removes all the actions from the menu bar.

See also removeAction().

### bool QMenuBar.event (self, QEvent)

### bool QMenuBar.eventFilter (self, QObject, QEvent)

### QMenuBar.focusInEvent (self, QFocusEvent)

### QMenuBar.focusOutEvent (self, QFocusEvent)

### int QMenuBar.heightForWidth (self, int)

### QMenuBar.initStyleOption (self, QStyleOptionMenuItem option, QAction action)

Initialize option with the values from the menu bar and information from action. This method is useful for subclasses when they need a QStyleOptionMenuItem, but don't want to fill in all the information themselves.

See also QStyleOption.initFrom() and QMenu.initStyleOption().

### QAction QMenuBar.insertMenu (self, 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 (self, 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 menu bar's list of actions before action before and returns it.

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

### bool QMenuBar.isDefaultUp (self)

### QMenuBar.keyPressEvent (self, QKeyEvent)

### QMenuBar.leaveEvent (self, QEvent)

### QSize QMenuBar.minimumSizeHint (self)

### QMenuBar.mouseMoveEvent (self, QMouseEvent)

### QMenuBar.mousePressEvent (self, QMouseEvent)

### QMenuBar.mouseReleaseEvent (self, QMouseEvent)

### QMenuBar.paintEvent (self, QPaintEvent)

### QMenuBar.resizeEvent (self, QResizeEvent)

### QMenuBar.setActiveAction (self, QAction action)

Sets the currently highlighted action to act.

This function was introduced in Qt 4.1.

See also activeAction().

### QMenuBar.setDefaultUp (self, bool)

### QMenuBar.setVisible (self, bool visible)

### QSize QMenuBar.sizeHint (self)

## Qt Signal Documentation

### void hovered (QAction *)

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

### void triggered (QAction *)

This signal is emitted when an action in a menu belonging to this menubar is triggered as a result of a mouse click; action is the action that caused the signal to be emitted.

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