MenuBar QML Type
提供一个窗口菜单栏。更多...
| Import Statement: | import QtQuick.Controls |
| Inherits: |
属性
方法
- void addMenu(Menu menu)
- void insertMenu(int index, Menu menu)
- Menu menuAt(int index)
- void removeMenu(Menu menu)
- Menu takeMenu(int index)
详细说明

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 还提供了 API,用于动态add 、insert 、remove 和take 菜单。可以通过menuAt() 访问菜单栏中的菜单。
原生菜单栏
自 Qt 6.8 起,MenuBar 在 macOS 上已作为原生菜单栏实现。因此,MenuBar 内的所有 Menus、MenuItems 和 MenuBarItems 也将成为原生组件。 虽然这具有界面外观完全原生的优势,但也存在一个缺点:即设置在上述控件上的委托将不会用于渲染。如果不需要原生 MenuBar,可以将QGuiApplication::setAttribute(Qt::AA_DontUseNativeMenuBar) 设置为禁用。
另请参阅 《Qt Quick Controls 》中的“自定义菜单栏”、“Menu ”、“MenuBarItem ”、“菜单控件”以及“焦点管理”。
属性文档
delegate : Component
该属性保存用于创建菜单栏项目以在菜单栏中显示菜单的组件。
MenuBar 不会获取该委托的所有权。
另请参阅 MenuBarItem 。
menus : list<Menu>
该属性包含菜单列表。
该列表包含所有在 QML 中作为菜单栏子元素声明的菜单,以及分别通过 `addMenu()` 和 `insertMenu()` 方法动态添加或插入的菜单。
方法文档
void addMenu(Menu menu)
将“menu ”添加到菜单列表的末尾。
void insertMenu(int index, Menu menu)
在index 处插入menu 。
Menu menuAt(int index)
返回位于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.