Qt 5.8 | Qt Quick Controls 2 | QML Types | Menu QML Type

Qt 5.8.0 Reference Documentation

# Menu QML Type

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

Import Statement: | import QtQuick.Controls 2.1
Since: | Qt 5.7

- List of all members, including inherited members

## Properties

- contentData : list<Object>
- contentModel : model
- title : string

## Methods

- void addItem(Item item)
- void insertItem(int index, Item item)
- Item itemAt(int index)
- void moveItem(int from, int to)
- void removeItem(int index)

## Detailed Description

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.

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

See also Customizing Menu, Menu Controls, and Popup Controls.

## Property Documentation

[default] contentData : list<Object>

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.

Note: Unlike `contentChildren`, `contentData` does include non-visual QML objects. It is not re-ordered when items are inserted or moved.

See also 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.

## Method Documentation

void addItem(Item item)

Adds item to the end of the list of items.

void insertItem(int index, Item item)

Inserts item at index.

Item itemAt(int index)

Returns the item at index, or `null` if it does not exist.

void moveItem(int from, int to)

Moves an item from one index to another.

void removeItem(int index)

Removes the item at index.

Note: The ownership of the item is transferred to the caller.