QMenuBar 类

QMenuBar 类提供水平菜单栏。 更多...

头: #include <QMenuBar>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QWidget

特性

公共函数

  QMenuBar (QWidget * parent = nullptr)
virtual ~QMenuBar ()
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 * addAction (const QString & text , const Obj * receiver , PointerToMemberFunctionOrFunctor method )
QAction * addAction (const QString & text , Functor functor )
QAction * addMenu (QMenu * menu )
QMenu * addMenu (const QString & title )
QMenu * addMenu (const QIcon & icon , const QString & title )
QAction * addSeparator ()
void clear ()
QWidget * cornerWidget (Qt::Corner corner = Qt::TopRightCorner) const
QAction * insertMenu (QAction * before , QMenu * menu )
QAction * insertSeparator (QAction * before )
bool isDefaultUp () const
bool isNativeMenuBar () const
void setActiveAction (QAction * act )
void setCornerWidget (QWidget * widget , Qt::Corner corner = Qt::TopRightCorner)
void setDefaultUp (bool)
void setNativeMenuBar (bool nativeMenuBar )
NSMenu * toNSMenu ()

重实现公共函数

virtual int heightForWidth (int) const override
virtual QSize minimumSizeHint () const override
virtual QSize sizeHint () const override

公共槽

virtual void setVisible (bool visible ) override

信号

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

保护函数

virtual void initStyleOption (QStyleOptionMenuItem * option , const QAction * action ) const

重实现保护函数

virtual void actionEvent (QActionEvent * e ) override
virtual void changeEvent (QEvent * e ) override
virtual bool event (QEvent * e ) override
virtual bool eventFilter (QObject * object , QEvent * event ) override
virtual void focusInEvent (QFocusEvent *) override
virtual void focusOutEvent (QFocusEvent *) override
virtual void keyPressEvent (QKeyEvent * e ) override
virtual void leaveEvent (QEvent *) override
virtual void mouseMoveEvent (QMouseEvent * e ) override
virtual void mousePressEvent (QMouseEvent * e ) override
virtual void mouseReleaseEvent (QMouseEvent * e ) override
virtual void paintEvent (QPaintEvent * e ) override
virtual void resizeEvent (QResizeEvent *) override
virtual void timerEvent (QTimerEvent * e ) override

详细描述

菜单栏由下拉菜单项列表组成。添加菜单项采用 addMenu ()。例如,假定 menubar 是指针指向 QMenuBar 和 fileMenu 是指针指向 QMenu ,以下语句将菜单插入菜单栏:

menubar->addMenu(fileMenu);
					

菜单项文本中的 & 和号,把 Alt+F 设为此菜单的快捷方式 (在菜单栏中,可以使用 && 以获得真正 & 和号)。

不需要布置菜单栏。它将自己的几何体自动设置到父级 Widget 的顶部,并适当改变它每当父级重置大小时。

用法

在大多数主窗口样式应用程序,将使用 menuBar() function provided in QMainWindow ,添加 QMenu 到菜单栏和添加 QAction 到弹出菜单。

范例 (来自 菜单 范例):

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

可以移除菜单项采用 removeAction ().

可以把 Widget 添加到菜单,通过使用实例化的 QWidgetAction 班来保持它们。然后,可以把这些动作以通常方式插入菜单;见 QMenu 文档编制了解更多细节。

从属平台的外观和感觉

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 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:

字符串匹配 Placement 注意事项
about.* Application Menu | About <application name> The application name is fetched from the Info.plist file (see note below). 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 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:

QMenuBar *menuBar = new QMenuBar(nullptr);
					

注意: 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 .

注意: 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 - 部署 了解更多信息。

注意: 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.

范例

菜单 范例展示如何使用 QMenuBar 和 QMenu 。其它 主窗口应用程序范例 还提供使用这些类的菜单。

另请参阅 QMenu , QShortcut , QAction , Apple 人机界面指南介绍 , GUI 设计手册:菜单栏 ,和 菜单范例 .

特性文档编制

defaultUp : bool

此特性保持弹出取向

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.

访问函数:

bool isDefaultUp () const
void setDefaultUp (bool)

nativeMenuBar : bool

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 false .

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.

访问函数:

bool isNativeMenuBar () const
void setNativeMenuBar (bool nativeMenuBar )

成员函数文档编制

QMenuBar:: QMenuBar ( QWidget * parent = nullptr)

构造菜单栏采用父级 parent .

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

此信号发射,当突显菜单动作时; action 是导致事件发送的动作。

这经常被用于更新状态信息。

另请参阅 triggered () 和 QAction::hovered ().

[override virtual slot] void QMenuBar:: setVisible ( bool visible )

重实现访问函数为特性: QWidget::visible .

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

此信号被发射当属于此菜单栏的菜单动作被触发作为鼠标点击结果时; action 是导致信号被发射的动作。

注意: QMenuBar 必须拥有所有权对于 QMenu 为使此信号工作。

通常,连接每个菜单动作到单个槽使用 QAction::triggered (),但有时想要将几项连接到单个槽 (最常见,若用户从数组中选择)。此信号在这种情况下很有用。

另请参阅 hovered () 和 QAction::triggered ().

[虚拟] QMenuBar:: ~QMenuBar ()

销毁菜单栏。

QAction *QMenuBar:: actionAt (const QPoint & pt ) const

返回 QAction at pt 。返回 nullptr 若没有动作在 pt 或者若位置拥有分隔符。

另请参阅 addAction () 和 addSeparator ().

[override virtual protected] void QMenuBar:: actionEvent ( QActionEvent * e )

重实现: QWidget::actionEvent (QActionEvent *event).

QRect QMenuBar:: actionGeometry ( QAction * act ) const

返回几何体对于动作 act 作为 QRect .

另请参阅 actionAt ().

QAction *QMenuBar:: activeAction () const

返回 QAction 目前有突显,若有的话,否则 nullptr .

另请参阅 setActiveAction ().

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

此方便函数创建新动作,采用 text 。函数将新近创建的动作添加到菜单的动作列表,并返回它。

另请参阅 QWidget::addAction () 和 QWidget::actions ().

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

这是重载函数。

此方便函数创建新动作采用给定 text 。动作的 triggered () 信号被连接到 receiver 's member slot. The function adds the newly created action to the menu's list of actions and returns it.

另请参阅 QWidget::addAction () 和 QWidget::actions ().

[since 5.11] template <typename Obj, typename PointerToMemberFunctionOrFunctor> QAction *QMenuBar:: addAction (const QString & text , const Obj * receiver , PointerToMemberFunctionOrFunctor method )

这是重载函数。

此方便函数创建新动作采用给定 text 。动作的 triggered () 信号被连接到 method receiver . The function adds the newly created action to the menu's list of actions and returns it.

QMenuBar 拥有所有权对于返回的 QAction .

该函数在 Qt 5.11 引入。

另请参阅 QWidget::addAction () 和 QWidget::actions ().

[since 5.11] template <typename Functor> QAction *QMenuBar:: addAction (const QString & text , Functor functor )

这是重载函数。

此方便函数创建新动作采用给定 text 。动作的 triggered () 信号被连接到 functor . The function adds the newly created action to the menu's list of actions and returns it.

QMenuBar 拥有所有权对于返回的 QAction .

该函数在 Qt 5.11 引入。

另请参阅 QWidget::addAction () 和 QWidget::actions ().

QAction *QMenuBar:: addMenu ( QMenu * menu )

追加 menu to the menu bar. Returns the menu's menuAction(). The menu bar does not take ownership of the menu.

注意: 返回的 QAction 对象可以用于隐藏相应菜单。

另请参阅 QWidget::addAction () 和 QMenu::menuAction ().

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

追加新的 QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

另请参阅 QWidget::addAction () 和 QMenu::menuAction ().

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

追加新的 QMenu with icon and title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

另请参阅 QWidget::addAction () 和 QMenu::menuAction ().

QAction *QMenuBar:: addSeparator ()

将分隔符追加到菜单。

[override virtual protected] void QMenuBar:: changeEvent ( QEvent * e )

重实现: QWidget::changeEvent (QEvent *event).

void QMenuBar:: clear ()

从菜单栏移除所有动作。

注意: 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.

另请参阅 removeAction ().

QWidget *QMenuBar:: cornerWidget ( Qt::Corner corner = Qt::TopRightCorner) const

返回第一菜单项左侧或最后菜单项右侧 Widget,从属 corner .

注意: 使用角落除了 Qt::TopRightCorner or Qt::TopLeftCorner 将导致警告。

另请参阅 setCornerWidget ().

[override virtual protected] bool QMenuBar:: event ( QEvent * e )

重实现: QWidget::event (QEvent *event).

[override virtual protected] bool QMenuBar:: eventFilter ( QObject * object , QEvent * event )

重实现: QObject::eventFilter (QObject *watched, QEvent *event).

[override virtual protected] void QMenuBar:: focusInEvent ( QFocusEvent *)

重实现: QWidget::focusInEvent (QFocusEvent *event).

[override virtual protected] void QMenuBar:: focusOutEvent ( QFocusEvent *)

重实现: QWidget::focusOutEvent (QFocusEvent *event).

[override virtual] int QMenuBar:: heightForWidth ( int ) const

重实现: QWidget::heightForWidth (int w) const.

[virtual protected] void QMenuBar:: initStyleOption ( QStyleOptionMenuItem * option , const QAction * action ) const

初始化 option 采用的值来自菜单栏和信息来自 action 。此方法对子类是有用的,当需要 QStyleOptionMenuItem ,但不希望自己填充所有信息。

另请参阅 QStyleOption::initFrom () 和 QMenu::initStyleOption ().

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

此方便函数插入 menu 先于动作 before 并返回菜单 menuAction()。

另请参阅 QWidget::insertAction () 和 addMenu ().

QAction *QMenuBar:: insertSeparator ( QAction * before )

此方便函数创建新分隔符动作,即:动作具有 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.

另请参阅 QWidget::insertAction () 和 addSeparator ().

[override virtual protected] void QMenuBar:: keyPressEvent ( QKeyEvent * e )

重实现: QWidget::keyPressEvent (QKeyEvent *event).

[override virtual protected] void QMenuBar:: leaveEvent ( QEvent *)

重实现: QWidget::leaveEvent (QEvent *event).

[override virtual] QSize QMenuBar:: minimumSizeHint () const

重实现访问函数为特性: QWidget::minimumSizeHint .

[override virtual protected] void QMenuBar:: mouseMoveEvent ( QMouseEvent * e )

重实现: QWidget::mouseMoveEvent (QMouseEvent *event).

[override virtual protected] void QMenuBar:: mousePressEvent ( QMouseEvent * e )

重实现: QWidget::mousePressEvent (QMouseEvent *event).

[override virtual protected] void QMenuBar:: mouseReleaseEvent ( QMouseEvent * e )

重实现: QWidget::mouseReleaseEvent (QMouseEvent *event).

[override virtual protected] void QMenuBar:: paintEvent ( QPaintEvent * e )

重实现: QWidget::paintEvent (QPaintEvent *event).

[override virtual protected] void QMenuBar:: resizeEvent ( QResizeEvent *)

重实现: QWidget::resizeEvent (QResizeEvent *event).

void QMenuBar:: setActiveAction ( QAction * act )

把目前突显动作设为 act .

另请参阅 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.

注意: 使用角落除了 Qt::TopRightCorner or Qt::TopLeftCorner 将导致警告。

另请参阅 cornerWidget ().

[override virtual] QSize QMenuBar:: sizeHint () const

重实现访问函数为特性: QWidget::sizeHint .

[override virtual protected] void QMenuBar:: timerEvent ( QTimerEvent * e )

重实现: QObject::timerEvent (QTimerEvent *event).

[since 5.2] NSMenu *QMenuBar:: toNSMenu ()

返回此菜单栏的本机 NSMenu。只可用于 macOS。

注意: 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.

该函数在 Qt 5.2 引入。