Menu QML Type

Menu popup that can be used as a context menu or popup menu. 更多...

import 语句: import QtQuick.Controls 2.2
Since: Qt 5.7
继承: Popup

特性

方法

详细描述

Menu has two main use cases:

  • Context menus; for example, a menu that is shown after right clicking
  • Popup menus; for example, a menu that is shown after clicking a button
Button {
    id: fileButton
    text: "File"
    onClicked: menu.open()
    Menu {
        id: menu
        y: fileButton.height
        MenuItem {
            text: "New..."
        }
        MenuItem {
            text: "Open..."
        }
        MenuItem {
            text: "Save"
        }
    }
}
					

Typically, menu items are statically declared as children of the menu, but Menu also provides API to add , insert , move and remove items dynamically. The items in a menu can be accessed using itemAt() or contentChildren .

尽管 MenuItems are most commonly used with Menu, it can contain any type of item.

另请参阅 Customizing Menu , 菜单控件 ,和 弹出控件 .

特性文档编制

[default] contentData : list < 对象 >

This property holds the list of content data.

The list contains all objects that have been declared in QML as children of the menu, and also items that have been dynamically added or inserted using the addItem() and insertItem() methods, respectively.

注意: 不像 contentChildren , contentData does include non-visual QML objects. It is not re-ordered when items are inserted or moved.

另请参阅 Item::data and contentChildren .

[read-only] contentModel : model

This property holds the model used to display menu items.

The content model is provided for visualization purposes. It can be assigned as a model to a content item that presents the contents of the menu.

Menu {
    id: menu
    contentItem: ListView {
        model: menu.contentModel
    }
}
							

The model allows menu items to be statically declared as children of the menu.

title : string

This property holds the title for the menu.

The title of a menu is often displayed in the text of a menu item when the menu is a submenu, and in the text of a tool button when it is in a menubar.


方法文档编制

void addItem ( Item item )

添加 item to the end of the list of items.

void insertItem ( int index , Item item )

插入 item at index .

Item itemAt ( int index )

返回项在 index ,或 null if it does not exist.

void moveItem ( int from , int to )

Moves an item from one index to another.

void removeItem ( int index )

移除项在 index .

注意: The ownership of the item is transferred to the caller.