QMenuBar Class
The QMenuBar class provides a row of menu titles, usually at the top of a window. More…
| Header | #include <QMenuBar> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QWidget |
List of all members, including inherited members
Public Functions
| QMenuBar(QWidget *parent = nullptr) | |
| ~QMenuBar() | |
| QAction * | addAction(const QString &text) |
| QAction * | addAction(const QString &text, Functor functor) |
| QAction * | addMenu(QMenu *menu) |
| QMenu * | addMenu(const QString &title) |
| QAction * | insertMenu(QAction *before, QMenu *menu) |
| void | clear() |
| QAction * | activeAction() const |
| void | setActiveAction(QAction *action) |
| QAction * | actionAt(const QPoint &point) const |
| QRect | actionGeometry(QAction *action) const |
Reimplemented Public Functions
| QSize | sizeHint() const |
| QSize | minimumSizeHint() const |
Signals
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | mouseMoveEvent(QMouseEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | leaveEvent(QEvent *event) |
| void | actionEvent(QActionEvent *event) |
| void | changeEvent(QEvent *event) |
Detailed Description
A menu bar shows the title of each of its menus. Clicking a title opens the menu below it; while one is open, moving over another title opens that one instead, and Left and Right in a menu go to the one next to it. A title that has no menu is a button that triggers its action. Put the bar at the top of the layout of the window:
QMenuBar *bar = new QMenuBar(window);
QMenu *file = bar->addMenu("&File");
file->addAction("&Open...", [] { openFile(); });
file->addSeparator();
file->addAction("&Quit", [] { QApplication::quit(); });
bar->addMenu("&Edit")->addAction("&Copy");
layout->addWidget(bar);
Alt and the letter after a & in a title opens its menu.

Differences from Qt
The bar does not become the menu bar of a window by itself, as the bar of a QMainWindow does: there is no QMainWindow, and no QLayout::setMenuBar(), so the bar is added to a layout like a widget. A bar is not a native menu bar, and there is no extension button for titles that do not fit: they are cut off. F10 and Alt alone do not move the focus to the bar, the titles have no icons, and the bar cannot be setCornerWidget()ed or setDefaultUp().
Member Function Documentation
QMenuBar::QMenuBar(QWidget *parent = nullptr)
Constructs a menu bar with the given parent.
QMenuBar::~QMenuBar()
Destroys the menu bar.
QAction * QMenuBar::actionAt(const QPoint &point) const
Returns the action whose title is at point in the bar; nullptr if there is none.
See also actionGeometry().
void QMenuBar::actionEvent(QActionEvent *event)
Reimplements QWidget::actionEvent().
QRect QMenuBar::actionGeometry(QAction *action) const
Returns the rectangle of the title of action in the bar; an empty rectangle if the action is not shown.
See also actionAt().
QAction * QMenuBar::activeAction() const
Returns the action whose title is highlighted; nullptr if none is.
See also setActiveAction().
QAction * QMenuBar::addAction(const QString &text)
Creates an action with the given text, adds it at the end of the bar, and returns it. The bar owns the action. An action with no menu is a title that triggers when it is clicked.
See also addMenu().
template <typename Functor>QAction * QMenuBar::addAction(const QString &text, Functor functor)
Creates an action with the given text, connects its triggered() signal to functor, adds the action at the end of the bar, and returns it.
QAction * QMenuBar::addMenu(QMenu *menu)
Adds menu at the end of the bar, and returns its action. The title of the menu is the text of the title in the bar. The bar does not own the menu.
See also insertMenu().
QMenu * QMenuBar::addMenu(const QString &title)
Creates a menu with the given title, adds it at the end of the bar, and returns it. The bar owns the menu.
See also addMenu(QMenu *).
void QMenuBar::changeEvent(QEvent *event)
Reimplements QWidget::changeEvent().
void QMenuBar::clear()
Removes all the actions of the bar. The actions and the menus that the bar owns are deleted.
void QMenuBar::hovered(QAction *action)
This signal is emitted when the user moves to an action of the menu bar or of one of its menus, with that action.
QAction * QMenuBar::insertMenu(QAction *before, QMenu *menu)
Adds menu in front of the action before, and returns its action.
See also addMenu().
void QMenuBar::leaveEvent(QEvent *event)
Reimplements QWidget::leaveEvent().
QSize QMenuBar::minimumSizeHint() const
Reimplements QWidget::minimumSizeHint().
void QMenuBar::mouseMoveEvent(QMouseEvent *event)
Reimplements QWidget::mouseMoveEvent().
void QMenuBar::mousePressEvent(QMouseEvent *event)
Reimplements QWidget::mousePressEvent().
void QMenuBar::paintEvent(QPaintEvent *event)
Reimplements QWidget::paintEvent().
void QMenuBar::setActiveAction(QAction *action)
Highlights the title of action, the one that has a menu open or that the mouse is on; nullptr for none. This emits hovered() but does not open the menu.
See also activeAction().
QSize QMenuBar::sizeHint() const
Reimplements QWidget::sizeHint().
void QMenuBar::triggered(QAction *action)
This signal is emitted when the user triggers an action of the menu bar or of one of its menus, with that action.