在本页

MenuBar QML Type

提供窗口菜单栏。更多

Import Statement: import QtQuick.Controls
Inherits:

Container

属性

方法

详细说明

带有文件、编辑和查看菜单的菜单栏

MenuBar 由下拉菜单组成,通常位于窗口的顶部边缘。

ApplicationWindow {
    id: window
    width: 320
    height: 260
    visible: true

    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            Action { text: qsTr("&New...") }
            Action { text: qsTr("&Open...") }
            Action { text: qsTr("&Save") }
            Action { text: qsTr("Save &As...") }
            MenuSeparator { }
            Action { text: qsTr("&Quit") }
        }
        Menu {
            title: qsTr("&Edit")
            Action { text: qsTr("Cu&t") }
            Action { text: qsTr("&Copy") }
            Action { text: qsTr("&Paste") }
        }
        Menu {
            title: qsTr("&Help")
            Action { text: qsTr("&About") }
        }
    }
}

通常,菜单会静态地声明为菜单栏的子菜单,但 MenuBar 也会动态地为add,insert,removetake 菜单提供 API。可以使用menuAt() 访问菜单栏中的菜单。

本地菜单栏

自 Qt 6.8 起,MenuBar 在 macOS 上作为原生菜单栏实现。因此,MenuBar 中的所有菜单、MenuItems 和 MenuBarItems 也都是原生的。这样做的好处是一切看起来都是原生的,但缺点是在上述控件上设置的委托不会被用于呈现。如果不希望使用本地 MenuBar,可以设置QGuiApplication::setAttribute(Qt::AA_DontUseNativeMenuBar) 来禁用它。

另请参阅 Customizing MenuBar(自定义 菜单栏)、MenuMenuBarItemMenu Controls菜单控件)和 Qt Quick Controls(控件)中的 Focus Management(焦点管理)。

属性文档

delegate : Component

该属性包含用于创建菜单栏项的组件,以便在菜单栏中显示菜单。

MenuBar 不拥有委托的所有权。

另请参阅 MenuBarItem

该属性包含菜单列表。

该列表包含已在 QML 中声明为菜单栏子菜单的所有菜单,以及分别使用addMenu() 和insertMenu() 方法动态添加或插入的菜单。

方法文档

void addMenu(Menu menu)

menu 添加到菜单列表末尾。

void insertMenu(int index, Menu menu)

index 插入menu

返回位于index 的菜单,如果不存在,则返回null

void removeMenu(Menu menu)

删除指定的menu 。如果菜单是open ,它将首先被dismissed 。当应用程序不再持有任何 QML 引用时,menu 最终会被垃圾回收器删除。

Menu takeMenu(int index)

删除并返回index 上的菜单。如果菜单是open ,它将首先被dismissed 。当应用程序不再持有任何 QML 引用时,菜单最终会被垃圾回收器删除。

© 2026 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.