本页内容

MenuItemGroup QML Type

用于管理原生菜单项的组。更多...

Import Statement: import Qt.labs.platform
Inherits:

QtObject

属性

信号

方法

详细说明

MenuItemGroup 将原生菜单项分组在一起。

MenuItemGroup 默认采用排他模式。在排他式菜单项组中,任何时候只能选中一个菜单项;选中另一个菜单项会自动取消先前选中的那个。MenuItemGroup 也可以配置为非排他模式,这对于将菜单项作为一个组来统一显示、隐藏、启用和禁用特别有用。

使用 MenuItemGroup 最直接的方法是为其分配一个项目列表。

Menu {
    id: verticalMenu
    title: qsTr("Vertical")

    MenuItemGroup {
        id: verticalGroup
        items: verticalMenu.items
    }

    MenuItem { text: qsTr("Top"); checkable: true }
    MenuItem { text: qsTr("Center"); checked: true }
    MenuItem { text: qsTr("Bottom"); checkable: true }
}

同一个菜单有时可能会包含不应包含在同一个排他性组中的菜单项。此类情况最好使用group 属性来处理。

Menu {
    id: horizontalMenu
    title: qsTr("Horizontal")

    MenuItemGroup {
        id: horizontalGroup
    }

    MenuItem {
        checked: true
        text: qsTr("Left")
        group: horizontalGroup
    }
    MenuItem {
        checkable: true
        text: qsTr("Center")
        group: horizontalGroup
    }
    MenuItem {
        text: qsTr("Right")
        checkable: true
        group: horizontalGroup
    }

    MenuItem { separator: true }
    MenuItem { text: qsTr("Justify"); checkable: true }
    MenuItem { text: qsTr("Absolute"); checkable: true }
}

更高级的使用场景可通过addItem()和removeItem()方法来处理。

注意: Qt.labs 模块中的类型在未来版本中不保证保持兼容性。

另请参阅 MenuItem 。

属性文档

checkedItem : MenuItem

该属性存储组中当前被选中的项目;如果未选中任何项目,则返回null 。

enabled : bool

该属性用于控制组是否处于启用状态。默认值为true 。

组的启用状态会影响组中每个项目的启用状态,但显式禁用的项目除外——即使组处于启用状态,这些项目也不会被启用。

exclusive : bool

该属性用于确定该组是否为排他组。默认值为true 。

在互斥的菜单项组中,任何时候只能选中一个项目;选中另一个项目会自动取消先前选中的项目。

items : list<MenuItem>

该属性存储了该组中的项目列表。

visible : bool

该属性控制组是否可见。默认值为true 。

组的可见性会影响组中每个项的可见性,但明确隐藏的项即使组可见也不会显示。

Signal 文档

hovered(MenuItem item)

当用户将鼠标悬停在组中的某个item 上时,会触发此信号。

注意: 相应的处理程序 是onHovered 。

另请参阅 MenuItem::hovered()。

triggered(MenuItem item)

当组中的item 被用户触发时,会发出此信号。

注意: 相应的处理程序 是onTriggered 。

另请参阅 MenuItem::triggered()。

方法文档

void addItem(MenuItem item)

将一个item 添加到组中。

void clear()

从组中移除所有项目。

void removeItem(MenuItem item)

将item 从组中移除。

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