|qmake:||QT += widgets|
|QMenuBar(QWidget *parent = Q_NULLPTR)|
|QAction *||actionAt(const QPoint &pt) const|
|QRect||actionGeometry(QAction *act) const|
|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)|
|QWidget *||cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const|
|QAction *||insertMenu(QAction *before, QMenu *menu)|
|QAction *||insertSeparator(QAction *before)|
|void||setCornerWidget(QWidget *widget, Qt::Corner corner = Qt::TopRightCorner)|
Reimplemented Public Functions
|virtual int||heightForWidth(int) const|
|virtual QSize||minimumSizeHint() const|
|virtual QSize||sizeHint() const|
- 211 public functions inherited from QWidget
- 31 public functions inherited from QObject
- 14 public functions inherited from QPaintDevice
|virtual void||setVisible(bool visible)|
|void||initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const|
Reimplemented Protected Functions
|virtual void||actionEvent(QActionEvent *e)|
|virtual void||changeEvent(QEvent *e)|
|virtual bool||event(QEvent *e)|
|virtual bool||eventFilter(QObject *object, QEvent *event)|
|virtual void||focusInEvent(QFocusEvent *)|
|virtual void||focusOutEvent(QFocusEvent *)|
|virtual void||keyPressEvent(QKeyEvent *e)|
|virtual void||leaveEvent(QEvent *)|
|virtual void||mouseMoveEvent(QMouseEvent *e)|
|virtual void||mousePressEvent(QMouseEvent *e)|
|virtual void||mouseReleaseEvent(QMouseEvent *e)|
|virtual void||paintEvent(QPaintEvent *e)|
|virtual void||resizeEvent(QResizeEvent *)|
|virtual void||timerEvent(QTimerEvent *e)|
- 35 protected functions inherited from QWidget
- 9 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice
Additional Inherited Members
- 5 static public members inherited from QWidget
- 11 static public members inherited from QObject
- 1 protected slot inherited from QWidget
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:
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.
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.
QMenuBar as a Global Menu Bar
On macOS and on certain Linux desktop environments such as Ubuntu Unity, QMenuBar 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 for macOS also provides a menu bar merging feature to make QMenuBar conform more closely to accepted macOS 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:
|about.*||Application Menu | About <application name>||The application name is fetched from the |
|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 QCoreApplication::quit()|
You can override this behavior by using the QAction::menuRole() property.
If you want all windows in a Mac application to share one menu bar, you must create a menu bar that does not have a parent. Create a parent-less menu bar this way:
Note: Do not call QMainWindow::menuBar() to create the shared menu bar, because that menu bar will have the QMainWindow as its parent. That menu bar would only be displayed for the parent QMainWindow.
Note: The text used for the application name in the macOS menu bar is obtained from the value set in the
Info.plist file in the application's bundle. See Qt for macOS - Deployment for more information.
Note: On Linux, if the com.canonical.AppMenu.Registrar service is available on the D-Bus session bus, then Qt will communicate with it to install the application's menus into the global menu bar, as described.
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.
This property holds whether or not a menubar will be used as a native menubar on platforms that support it.
This property specifies whether or not the menubar should be used as a native menubar on platforms that support it. The currently supported platforms are macOS, and Linux desktops which use the com.canonical.dbusmenu D-Bus interface (such as Ubuntu Unity). If this property is
true, the menubar is used in the native menubar and is not in the window of its parent; if
false the menubar remains in the window. On other platforms, setting this attribute has no effect, and reading this attribute will always return
The default is to follow whether the Qt::AA_DontUseNativeMenuBar attribute is set for the application. Explicitly setting this property overrides the presence (or absence) of the attribute.
This property was introduced in Qt 4.6.
Member Function Documentation
QMenuBar::QMenuBar(QWidget *parent = Q_NULLPTR)
Constructs a menu bar with parent parent.
Destroys the menu bar.
Returns the QAction at pt. Returns 0 if there is no action at pt or if the location has a separator.
[virtual protected] void QMenuBar::actionEvent(QActionEvent *e)
Reimplemented from QWidget::actionEvent().
Returns the geometry of action act as a QRect.
See also actionAt().
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().
This is an overloaded function.
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.
This is an overloaded function.
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.
Appends menu to the menu bar. Returns the menu's menuAction().
Note: The returned QAction object can be used to hide the corresponding menu.
Appends a new QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.
Appends a new QMenu with icon and title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.
Appends a separator to the menu.
[virtual protected] void QMenuBar::changeEvent(QEvent *e)
Reimplemented from QWidget::changeEvent().
Removes all the actions from the menu bar.
Note: On macOS, menu items that have been merged to the system menu bar are not removed by this function. One way to handle this would be to remove the extra actions yourself. You can set the menu role on the different menus, so that you know ahead of time which menu items get merged and which do not. Then decide what to recreate or remove yourself.
See also removeAction().
QWidget *QMenuBar::cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const
Returns the widget on the left of the first or on the right of the last menu item, depending on corner.
See also setCornerWidget().
[virtual protected] bool QMenuBar::event(QEvent *e)
Reimplemented from QObject::event().
Reimplemented from QObject::eventFilter().
Reimplemented from QWidget::focusInEvent().
Reimplemented from QWidget::focusOutEvent().
Reimplemented from QWidget::heightForWidth().
[signal] void QMenuBar::hovered(QAction *action)
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.
[protected] void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
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.
This convenience function inserts menu before action before and returns the menus menuAction().
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.
[virtual protected] void QMenuBar::keyPressEvent(QKeyEvent *e)
Reimplemented from QWidget::keyPressEvent().
Reimplemented from QWidget::leaveEvent().
[virtual] QSize QMenuBar::minimumSizeHint() const
Reimplemented from QWidget::minimumSizeHint().
[virtual protected] void QMenuBar::mouseMoveEvent(QMouseEvent *e)
Reimplemented from QWidget::mouseMoveEvent().
[virtual protected] void QMenuBar::mousePressEvent(QMouseEvent *e)
Reimplemented from QWidget::mousePressEvent().
[virtual protected] void QMenuBar::mouseReleaseEvent(QMouseEvent *e)
Reimplemented from QWidget::mouseReleaseEvent().
[virtual protected] void QMenuBar::paintEvent(QPaintEvent *e)
Reimplemented from QWidget::paintEvent().
Reimplemented from QWidget::resizeEvent().
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::setCornerWidget(QWidget *widget, Qt::Corner corner = Qt::TopRightCorner)
This sets the given widget to be shown directly on the left of the first menu item, or on the right of the last menu item, depending on corner.
The menu bar takes ownership of widget, reparenting it into the menu bar. However, if the corner already contains a widget, this previous widget will no longer be managed and will still be a visible child of the menu bar.
See also cornerWidget().
Reimplemented from QWidget::setVisible().
[virtual] QSize QMenuBar::sizeHint() const
Reimplemented from QWidget::sizeHint().
[virtual protected] void QMenuBar::timerEvent(QTimerEvent *e)
Reimplemented from QObject::timerEvent().
Returns the native NSMenu for this menu bar. Available on macOS only.
Note: Qt may set the delegate on the native menu bar. If you need to set your own delegate, make sure you save the original one and forward any calls to it.
This function was introduced in Qt 5.2.
[signal] void QMenuBar::triggered(QAction *action)
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.
© 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.