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) 来禁用它。

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

属性文档

contentHeight : real [since QtQuick.Controls 2.3 (Qt 5.10)]

该属性用于保存内容高度。它用于计算菜单栏的总隐含高度。

注: QtQuick.Controls 2.3 (Qt 5.10) 起,该属性在MenuBar 中可用,但在QtQuick.Controls 2.5 (Qt 5.12) 中,该属性被提升为容器基本类型。

该属性在 QtQuick.Controls 2.3 (Qt 5.10) 中引入。

另请参阅 Container::contentHeight


contentWidth : real [since QtQuick.Controls 2.3 (Qt 5.10)]

该属性保存内容宽度。它用于计算菜单栏的总隐含宽度。

注: QtQuick.Controls 2.3 (Qt 5.10) 起,该属性在MenuBar 中可用,但在QtQuick.Controls 2.5 (Qt 5.12) 中,该属性被提升为容器基本类型。

该属性在 QtQuick.Controls 2.3 (Qt 5.10) 中引入。

另请参阅 Container::contentWidth


delegate : Component

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

另请参见 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 引用时,该菜单最终会被垃圾回收器删除。


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