CopperSpice API  1.9.1
QMenuBar Class Reference

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

Inheritance diagram for QMenuBar:
QWidget QObject QPaintDevice

Public Signals

void hovered (QAction *action)
 
void triggered (QAction *action)
 
- Public Signals inherited from QWidget
void customContextMenuRequested (const QPoint &pos)
 
void windowIconChanged (const QIcon &icon)
 
void windowIconTextChanged (const QString &iconText)
 
void windowTitleChanged (const QString &title)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void setVisible (bool visible)
 
- Public Slots inherited from QWidget
bool close ()
 
void hide ()
 
void lower ()
 
void raise ()
 
void repaint ()
 
void setDisabled (bool disable)
 
void setEnabled (bool enable)
 
void setFocus ()
 
void setHidden (bool hidden)
 
void setStyleSheet (const QString &styleSheet)
 
virtual void setVisible (bool visible)
 
void setWindowModified (bool modified)
 
void setWindowTitle (const QString &title)
 
void show ()
 
void showFullScreen ()
 
void showMaximized ()
 
void showMinimized ()
 
void showNormal ()
 
void update ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QMenuBar (QWidget *parent=nullptr)
 
 ~QMenuBar ()
 
QActionactionAt (const QPoint &point) const
 
QRect actionGeometry (QAction *action) const
 
QActionactiveAction () const
 
QActionaddAction (const QString &text)
 
QActionaddAction (const QString &text, const QObject *receiver, const QString &member)
 
void addAction (QAction *action)
 
QMenuaddMenu (const QIcon &icon, const QString &title)
 
QMenuaddMenu (const QString &title)
 
QActionaddMenu (QMenu *menu)
 
QActionaddSeparator ()
 
void clear ()
 
QWidgetcornerWidget (Qt::Corner corner=Qt::TopRightCorner) const
 
int heightForWidth (int width) const override
 
QActioninsertMenu (QAction *before, QMenu *menu)
 
QActioninsertSeparator (QAction *before)
 
bool isDefaultUp () const
 
bool isNativeMenuBar () const
 
QSize minimumSizeHint () const override
 
QPlatformMenuBarplatformMenuBar ()
 
void setActiveAction (QAction *action)
 
void setCornerWidget (QWidget *widget, Qt::Corner corner=Qt::TopRightCorner)
 
void setDefaultUp (bool enabled)
 
void setNativeMenuBar (bool nativeMenuBar)
 
QSize sizeHint () const override
 
NSMenu * toNSMenu ()
 
- Public Methods inherited from QWidget
 QWidget (QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::EmptyFlag)
 
 ~QWidget ()
 
bool acceptDrops () const
 
QString accessibleDescription () const
 
QString accessibleName () const
 
QList< QAction * > actions () const
 
void activateWindow ()
 
void addAction (QAction *action)
 
void addActions (const QList< QAction * > &actions)
 
void adjustSize ()
 
bool autoFillBackground () const
 
QPalette::ColorRole backgroundRole () const
 
QBackingStorebackingStore () const
 
QSize baseSize () const
 
QWidget * childAt (const QPoint &position) const
 
QWidget * childAt (int x, int y) const
 
QRect childrenRect () const
 
QRegion childrenRegion () const
 
void clearFocus ()
 
void clearMask ()
 
QMargins contentsMargins () const
 
QRect contentsRect () const
 
Qt::ContextMenuPolicy contextMenuPolicy () const
 
QCursor cursor () const
 
WId effectiveWinId () const
 
void ensurePolished () const
 
Qt::FocusPolicy focusPolicy () const
 
QWidget * focusProxy () const
 
QWidget * focusWidget () const
 
const QFontfont () const
 
QFontInfo fontInfo () const
 
QFontMetrics fontMetrics () const
 
QPalette::ColorRole foregroundRole () const
 
QRect frameGeometry () const
 
QSize frameSize () const
 
const QRectgeometry () const
 
void getContentsMargins (int *left, int *top, int *right, int *bottom) const
 
QPixmap grab (const QRect &rectangle=QRect (QPoint (0, 0), QSize (-1,-1)))
 
void grabGesture (Qt::GestureType gestureType, Qt::GestureFlags flags=Qt::GestureFlags ())
 
void grabKeyboard ()
 
void grabMouse ()
 
void grabMouse (const QCursor &cursor)
 
int grabShortcut (const QKeySequence &key, Qt::ShortcutContext context=Qt::WindowShortcut)
 
QGraphicsEffectgraphicsEffect () const
 
QGraphicsProxyWidgetgraphicsProxyWidget () const
 
bool hasEditFocus () const
 
bool hasFocus () const
 
virtual bool hasHeightForWidth () const
 
bool hasMouseTracking () const
 
int height () const
 
Qt::InputMethodHints inputMethodHints () const
 
virtual QVariant inputMethodQuery (Qt::InputMethodQuery query) const
 
void insertAction (QAction *before, QAction *action)
 
void insertActions (QAction *before, QList< QAction * > actions)
 
bool isActiveWindow () const
 
bool isAncestorOf (const QWidget *child) const
 
bool isEnabled () const
 
bool isEnabledTo (const QWidget *parent) const
 
bool isEnabledToTLW () const
 
bool isFullScreen () const
 
bool isHidden () const
 
bool isMaximized () const
 
bool isMinimized () const
 
bool isModal () const
 
bool isTopLevel () const
 
bool isVisible () const
 
bool isVisibleTo (const QWidget *parent) const
 
bool isWindow () const
 
bool isWindowModified () const
 
QLayoutlayout () const
 
Qt::LayoutDirection layoutDirection () const
 
QLocale locale () const
 
QPoint mapFrom (const QWidget *parent, const QPoint &pos) const
 
QPoint mapFromGlobal (const QPoint &pos) const
 
QPoint mapFromParent (const QPoint &pos) const
 
QPoint mapTo (const QWidget *parent, const QPoint &pos) const
 
QPoint mapToGlobal (const QPoint &pos) const
 
QPoint mapToParent (const QPoint &pos) const
 
QRegion mask () const
 
int maximumHeight () const
 
QSize maximumSize () const
 
int maximumWidth () const
 
int minimumHeight () const
 
QSize minimumSize () const
 
int minimumWidth () const
 
void move (const QPoint &point)
 
void move (int x, int y)
 
QWidget * nativeParentWidget () const
 
QWidget * nextInFocusChain () const
 
QRect normalGeometry () const
 
void overrideWindowFlags (Qt::WindowFlags flags)
 
QPaintEnginepaintEngine () const override
 
const QPalettepalette () const
 
QWidget * parentWidget () const
 
QPoint pos () const
 
QWidget * previousInFocusChain () const
 
QRect rect () const
 
void releaseKeyboard ()
 
void releaseMouse ()
 
void releaseShortcut (int id)
 
void removeAction (QAction *action)
 
void render (QPaintDevice *target, const QPoint &targetOffset=QPoint (), const QRegion &sourceRegion=QRegion (), RenderFlags renderFlags=RenderFlags (DrawWindowBackground|DrawChildren))
 
void render (QPainter *painter, const QPoint &targetOffset=QPoint (), const QRegion &sourceRegion=QRegion (), RenderFlags renderFlags=RenderFlags (DrawWindowBackground|DrawChildren))
 
void repaint (const QRect &rect)
 
void repaint (const QRegion &region)
 
void repaint (int x, int y, int w, int h)
 
void resize (const QSize &size)
 
void resize (int w, int h)
 
bool restoreGeometry (const QByteArray &geometry)
 
QByteArray saveGeometry () const
 
void scroll (int dx, int dy)
 
void scroll (int dx, int dy, const QRect &rect)
 
void setAcceptDrops (bool on)
 
void setAccessibleDescription (const QString &description)
 
void setAccessibleName (const QString &name)
 
void setAttribute (Qt::WidgetAttribute attribute, bool enable=true)
 
void setAutoFillBackground (bool enable)
 
void setBackgroundRole (QPalette::ColorRole role)
 
void setBaseSize (const QSize &size)
 
void setBaseSize (int basew, int baseh)
 
void setContentsMargins (const QMargins &margins)
 
void setContentsMargins (int left, int top, int right, int bottom)
 
void setContextMenuPolicy (Qt::ContextMenuPolicy policy)
 
void setCursor (const QCursor &cursor)
 
void setEditFocus (bool enable)
 
void setFixedHeight (int h)
 
void setFixedSize (const QSize &size)
 
void setFixedSize (int w, int h)
 
void setFixedWidth (int w)
 
void setFocus (Qt::FocusReason reason)
 
void setFocusPolicy (Qt::FocusPolicy policy)
 
void setFocusProxy (QWidget *widget)
 
void setFont (const QFont &font)
 
void setForegroundRole (QPalette::ColorRole role)
 
void setGeometry (const QRect &rect)
 
void setGeometry (int x, int y, int w, int h)
 
void setGraphicsEffect (QGraphicsEffect *effect)
 
void setInputMethodHints (Qt::InputMethodHints hints)
 
void setLayout (QLayout *layout)
 
void setLayoutDirection (Qt::LayoutDirection direction)
 
void setLocale (const QLocale &locale)
 
void setMask (const QBitmap &bitmap)
 
void setMask (const QRegion &region)
 
void setMaximumHeight (int maxh)
 
void setMaximumSize (const QSize &size)
 
void setMaximumSize (int maxw, int maxh)
 
void setMaximumWidth (int maxw)
 
void setMinimumHeight (int minh)
 
void setMinimumSize (const QSize &size)
 
void setMinimumSize (int minw, int minh)
 
void setMinimumWidth (int minw)
 
void setMouseTracking (bool enable)
 
void setPalette (const QPalette &palette)
 
void setParent (QWidget *parent)
 
void setParent (QWidget *parent, Qt::WindowFlags flags)
 
void setShortcutAutoRepeat (int id, bool enable=true)
 
void setShortcutEnabled (int id, bool enable=true)
 
void setSizeIncrement (const QSize &size)
 
void setSizeIncrement (int w, int h)
 
void setSizePolicy (QSizePolicy policy)
 
void setSizePolicy (QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical)
 
void setStatusTip (const QString &data)
 
void setStyle (QStyle *style)
 
void setToolTip (const QString &data)
 
void setToolTipDuration (int msec)
 
void setUpdatesEnabled (bool enable)
 
void setWhatsThis (const QString &str)
 
void setWindowFilePath (const QString &filePath)
 
void setWindowFlags (Qt::WindowFlags flags)
 
void setWindowIcon (const QIcon &icon)
 
void setWindowModality (Qt::WindowModality windowModality)
 
void setWindowOpacity (qreal level)
 
void setWindowRole (const QString &role)
 
void setWindowState (Qt::WindowStates windowState)
 
QSize size () const
 
QSize sizeIncrement () const
 
QSizePolicy sizePolicy () const
 
void stackUnder (QWidget *widget)
 
QString statusTip () const
 
QStylestyle () const
 
QString styleSheet () const
 
bool testAttribute (Qt::WidgetAttribute attribute) const
 
QString toolTip () const
 
int toolTipDuration () const
 
QWidget * topLevelWidget () const
 
bool underMouse () const
 
void ungrabGesture (Qt::GestureType gestureType)
 
void unsetCursor ()
 
void unsetLayoutDirection ()
 
void unsetLocale ()
 
void update (const QRect &rect)
 
void update (const QRegion &region)
 
void update (int x, int y, int w, int h)
 
void updateGeometry ()
 
bool updatesEnabled () const
 
QRegion visibleRegion () const
 
QString whatsThis () const
 
int width () const
 
QWidget * window () const
 
QString windowFilePath () const
 
Qt::WindowFlags windowFlags () const
 
QWindowwindowHandle () const
 
QIcon windowIcon () const
 
Qt::WindowModality windowModality () const
 
qreal windowOpacity () const
 
QString windowRole () const
 
Qt::WindowStates windowState () const
 
QString windowTitle () const
 
Qt::WindowType windowType () const
 
WId winId () const
 
int x () const
 
int y () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 
- Public Methods inherited from QPaintDevice
virtual ~QPaintDevice ()
 
int colorCount () const
 
int depth () const
 
int devicePixelRatio () const
 
qreal devicePixelRatioF () const
 
int height () const
 
int heightMM () const
 
int logicalDpiX () const
 
int logicalDpiY () const
 
bool paintingActive () const
 
int physicalDpiX () const
 
int physicalDpiY () const
 
int width () const
 
int widthMM () const
 

Protected Methods

void actionEvent (QActionEvent *event) override
 
void changeEvent (QEvent *event) override
 
bool event (QEvent *event) override
 
bool eventFilter (QObject *object, QEvent *event) override
 
void focusInEvent (QFocusEvent *event) override
 
void focusOutEvent (QFocusEvent *event) override
 
void initStyleOption (QStyleOptionMenuItem *option, const QAction *action) const
 
void keyPressEvent (QKeyEvent *event) override
 
void leaveEvent (QEvent *event) override
 
void mouseMoveEvent (QMouseEvent *event) override
 
void mousePressEvent (QMouseEvent *event) override
 
void mouseReleaseEvent (QMouseEvent *event) override
 
void paintEvent (QPaintEvent *event) override
 
void resizeEvent (QResizeEvent *event) override
 
void timerEvent (QTimerEvent *event) override
 
- Protected Methods inherited from QWidget
virtual void closeEvent (QCloseEvent *event)
 
virtual void contextMenuEvent (QContextMenuEvent *event)
 
void create (WId window=0, bool initializeWindow=true, bool destroyOldWindow=true)
 
void destroy (bool destroyWindow=true, bool destroySubWindows=true)
 
virtual void dragEnterEvent (QDragEnterEvent *event)
 
virtual void dragLeaveEvent (QDragLeaveEvent *event)
 
virtual void dragMoveEvent (QDragMoveEvent *event)
 
virtual void dropEvent (QDropEvent *event)
 
virtual void enterEvent (QEvent *event)
 
bool event (QEvent *event) override
 
bool focusNextChild ()
 
virtual bool focusNextPrevChild (bool next)
 
bool focusPreviousChild ()
 
virtual void hideEvent (QHideEvent *event)
 
void initPainter (QPainter *painter) const override
 
virtual void inputMethodEvent (QInputMethodEvent *event)
 
virtual void keyReleaseEvent (QKeyEvent *event)
 
int metric (PaintDeviceMetric metric) const override
 
virtual void mouseDoubleClickEvent (QMouseEvent *event)
 
virtual void moveEvent (QMoveEvent *event)
 
virtual bool nativeEvent (const QByteArray &eventType, void *message, long *result)
 
virtual void showEvent (QShowEvent *event)
 
virtual void tabletEvent (QTabletEvent *event)
 
virtual void wheelEvent (QWheelEvent *event)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Protected Methods inherited from QPaintDevice
 QPaintDevice ()
 

Properties

 defaultUp
 
 nativeMenuBar
 
- Properties inherited from QWidget
 acceptDrops
 
 accessibleDescription
 
 accessibleName
 
 autoFillBackground
 
 baseSize
 
 childrenRect
 
 childrenRegion
 
 contextMenuPolicy
 
 cursor
 
 enabled
 
 focus
 
 focusPolicy
 
 font
 
 frameGeometry
 
 frameSize
 
 fullScreen
 
 geometry
 
 height
 
 inputMethodHints
 
 isActiveWindow
 
 layoutDirection
 
 locale
 
 maximized
 
 maximumHeight
 
 maximumSize
 
 maximumWidth
 
 minimized
 
 minimumHeight
 
 minimumSize
 
 minimumSizeHint
 
 minimumWidth
 
 modal
 
 mouseTracking
 
 normalGeometry
 
 palette
 
 pos
 
 rect
 
 size
 
 sizeHint
 
 sizeIncrement
 
 sizePolicy
 
 statusTip
 
 styleSheet
 
 toolTip
 
 updatesEnabled
 
 visible
 
 whatsThis
 
 width
 
 windowFilePath
 
 windowFlags
 
 windowIcon
 
 windowIconText
 
 windowModality
 
 windowModified
 
 windowOpacity
 
 windowTitle
 
 x
 
 y
 
- Properties inherited from QObject
 objectName
 

Friends

class QMenu
 
class QWindowsStyle
 

Additional Inherited Members

- Public Typedefs inherited from QWidget
using RenderFlags = QFlags< RenderFlag >
 
- Public Types inherited from QWidget
enum  RenderFlag
 
- Public Types inherited from QPaintDevice
enum  PaintDeviceMetric
 
- Static Public Methods inherited from QWidget
static QWidget * createWindowContainer (QWindow *window, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::EmptyFlag)
 
static QWidget * find (WId id)
 
static QWidget * keyboardGrabber ()
 
static QWidget * mouseGrabber ()
 
static void setTabOrder (QWidget *firstWidget, QWidget *secondWidget)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Slots inherited from QWidget
void updateMicroFocus ()
 

Detailed Description

The QMenuBar class provides a horizontal menu bar. New menu items can be addedc to the menu bar by calling the addMenu() method. There is no need to lay out a menu bar. The geometry is adjusted automatically to match the parent widget.

Example 1

The following code creates two new menus and then adds them to a new menu bar.

QMenu *fileMenu = new *QMenu;
QMenu *helpMenu = new *QMenu;
QMenuBar *menubar = new QMenuBar(nullptr);
menubar->addMenu(fileMenu);
menubar->addMenu(helpeMenu);

Example 2

For a main window in a GUI application which inherits form QMainWindow, you can simple call the QMainWindow::menuBar() method to add a new menu item to the menu bar. Menu items may be removed by calling removeAction(). The ampersand in the menu file text sets Alt+F as a shortcut.

Widgets can be added to menus by using the QWidgetAction class.

QMenu *fileMenu = menuBar()->addMenu("&File");
fileMenu->addAction(openFile);
QMenu *helpMenu = menuBar()->addMenu("&Help");
helpMenu->addAction(about);

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 on Mac OS X

QMenuBar on Mac OS X 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.

CopperSpice for Mac OS X 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.

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. Refer to Deploying an Application on Mac OS X for more information.

String MatchesPlacementNotes
about* About YourAppName The application name is obtained from the Info.plist file, if it is not found then no About item will appear
config, options, setup,
settings, preferences
Preferences If not found the Settings item will be disabled
quit, exit Quit application If 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 want every window in a Mac OS X application to share one menu bar, do not use QMainWindow::menuBar(). The menu bar it creates will have this QMainWindow as its parent. Instead, create a menu bar without a parent which can be shared among all the windows.

See also
QMenu, QShortcut, QAction

Constructor & Destructor Documentation

QMenuBar::QMenuBar ( QWidget parent = nullptr)
explicit

Constructs a menu bar with the given parent.

QMenuBar::~QMenuBar ( )

Destroys the menu bar.

Method Documentation

QAction * QMenuBar::actionAt ( const QPoint point) const

Returns the QAction at point. Returns nullptr if there is no action at point or if the location has a separator.

See also
addAction(), addSeparator()
void QMenuBar::actionEvent ( QActionEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::actionEvent()

QRect QMenuBar::actionGeometry ( QAction action) const

Returns the geometry of action 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()
QAction * QMenuBar::addAction ( const QString text)

Creates a new action using text and adds the action to the end of the current QMenu. Returns a pointer to the new QAction.

See also
QWidget::addAction(), QWidget::actions()
QAction * QMenuBar::addAction ( const QString text,
const QObject receiver,
const QString member 
)

Creates a new action using text and adds the action to the end of the current QMenu. Returns a pointer to the new QAction. The QAction triggered() signal is connected to the given member in the receiver.

See also
QWidget::addAction(), QWidget::actions()
void QWidget::addAction ( QAction action)
using

Appends action to the list of existing actions available for this widget. A QAction list is typically used to create a context QMenu, however it can be represented in different ways depending on the widget.

A QWidget should only have one of each action. Adding an action which already exists will not cause the same action to be in the widget multiple times. The ownership of action is not transferred to this QWidget.

See also
removeAction(), insertAction(), actions(), QMenu
QMenu * QMenuBar::addMenu ( const QIcon icon,
const QString title 
)

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(), QMenu::menuAction()
QMenu * QMenuBar::addMenu ( const QString title)

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(), QMenu::menuAction()
QAction * QMenuBar::addMenu ( QMenu menu)

Appends menu to the menu bar. Returns the menu's menuAction(). The returned QAction object can be used to hide the corresponding menu.

See also
QWidget::addAction(), QMenu::menuAction()
QAction * QMenuBar::addSeparator ( )

Appends a separator to the menu.

void QMenuBar::changeEvent ( QEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::changeEvent()

void QMenuBar::clear ( )

Removes all the actions from the menu bar.

Note
On Mac OS X, 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. Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner will result in a warning.

See also
setCornerWidget()
bool QMenuBar::event ( QEvent event)
overrideprotected

Reimplemented from QWidget::event()

bool QMenuBar::eventFilter ( QObject object,
QEvent event 
)
overrideprotected

Reimplemented from QWidget::eventFilter()

void QMenuBar::focusInEvent ( QFocusEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::focusInEvent()

void QMenuBar::focusOutEvent ( QFocusEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::focusOutEvent()

int QMenuBar::heightForWidth ( int  width) const
overridevirtual

Reimplemented from QWidget::heightForWidth()

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. This is used to update status information.

See also
triggered(), QAction::hovered()
void QMenuBar::initStyleOption ( QStyleOptionMenuItem option,
const QAction action 
) const
protected

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 do not want to fill in all the information themselves.

See also
QStyleOption::initFrom(), QMenu::initStyleOption()
QAction * QMenuBar::insertMenu ( QAction before,
QMenu menu 
)

Inserts menu before action before and returns the menus menuAction().

See also
QWidget::insertAction(), addMenu()
QAction * QMenuBar::insertSeparator ( QAction before)

Inserts a newly created action into this menu bar's list of actions before action before and returns a pointer to the QAction. This new action will return true if QAction::isSeparator() is called.

See also
QWidget::insertAction(), addSeparator()
bool QMenuBar::isDefaultUp ( ) const

Returns the value of the property.

bool QMenuBar::isNativeMenuBar ( ) const

Returns the value of the property.

void QMenuBar::keyPressEvent ( QKeyEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::keyPressEvent()

void QMenuBar::leaveEvent ( QEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::leaveEvent()

QSize QMenuBar::minimumSizeHint ( ) const
overridevirtual

Reimplemented from QWidget::minimumSizeHint()

void QMenuBar::mouseMoveEvent ( QMouseEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::mouseMoveEvent()

void QMenuBar::mousePressEvent ( QMouseEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::mousePressEvent()

void QMenuBar::mouseReleaseEvent ( QMouseEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::mouseReleaseEvent()

void QMenuBar::paintEvent ( QPaintEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::paintEvent()

QPlatformMenuBar * QMenuBar::platformMenuBar ( )

Returns a pointer to the internal platform menu bar.

void QMenuBar::resizeEvent ( QResizeEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::resizeEvent()

void QMenuBar::setActiveAction ( QAction action)

Sets the currently highlighted action to action.

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.

Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner will result in a warning.

See also
cornerWidget()
void QMenuBar::setDefaultUp ( bool  enabled)

Sets the value of the property to enabled.

void QMenuBar::setNativeMenuBar ( bool  nativeMenuBar)

Sets the value of the property to nativeMenuBar.

void QMenuBar::setVisible ( bool  visible)
slot

Sets the value of the property to visible.

QSize QMenuBar::sizeHint ( ) const
overridevirtual

Reimplemented from QWidget::sizeHint()

void QMenuBar::timerEvent ( QTimerEvent event)
overrideprotected

Reimplemented from QObject::timerEvent()

NSMenu * QMenuBar::toNSMenu ( )

Returns this menu bar as an NSMenu object. Only available on Mac OS X.

void QMenuBar::triggered ( QAction action)
signal

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(). It is also possible to connect several items to a single slot, most often if the user selects from an array.

Note
QMenuBar has to have ownership of the QMenu in order this signal to work.
See also
hovered(), QAction::triggered()

Property Documentation

QMenuBar::defaultUp

This property holds the popup orientation. By default menus will be bed display flowing down the screen. By setting the property to true the menu will appear to pop up the screen.

If the menu would not fit on the screen the other direction is used automatically.

PropertiesClass Methods
read isDefaultUp
write setDefaultUp
QMenuBar::nativeMenuBar

This property holds whether or not a menubar will be used as a native menubar on platforms that support it. This is currently supported only on Mac OS X.

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 the attribute will return false.

The default for this property is based on the Qt::AA_DontUseNativeMenuBar attribute in QApplication. Explicitly setting this property overrides the presence (or absence) of the attribute.

PropertiesClass Methods
read isNativeMenuBar
write setNativeMenuBar