Menu QML Type

Provides a menu component for use in menu bars, as context menu, and other popup menus. More...

Import Statement: import QtQuick.Controls 1.3
Since: Qt 5.1

Properties

Methods

Detailed Description

Menu {
    title: "Edit"

    MenuItem {
        text: "Cut"
        shortcut: "Ctrl+X"
        onTriggered: ...
    }

    MenuItem {
        text: "Copy"
        shortcut: "Ctrl+C"
        onTriggered: ...
    }

    MenuItem {
        text: "Paste"
        shortcut: "Ctrl+V"
        onTriggered: ...
    }

    MenuSeparator { }

    Menu {
        title: "More Stuff"

        MenuItem {
            text: "Do Nothing"
        }
    }
}

The main uses for menus:

  • as a top-level menu in a MenuBar
  • as a submenu inside another menu
  • as a standalone or context menu

Note that some properties, such as enabled, text, or iconSource, only make sense in a particular use case of the menu.

See also MenuBar, MenuItem, and MenuSeparator.

Property Documentation

enabled : bool

Whether the menu is enabled, and responsive to user interaction as a submenu. Its value defaults to true.


iconName : string

Sets the icon name for the menu icon. This will pick the icon with the given name from the current theme. Only works as a submenu.

Its value defaults to the empty string.

See also iconSource.


iconSource : url

Sets the icon file or resource url for the menu icon as a submenu. Defaults to the empty URL.

See also iconName.


defaultitems : list<Object>

The list of items in the menu.

Menu only accepts objects of type Menu, MenuItem, and MenuSeparator as children. It also supports Instantiator objects as long as the insertion is being done manually using insertItem().

Menu {
    id: recentFilesMenu

    Instantiator {
        model: recentFilesModel
        MenuItem {
            text: model.fileName
        }
        onObjectAdded: recentFilesMenu.insertItem(index, object)
        onObjectRemoved: recentFilesMenu.removeItem(object)
    }

    MenuSeparator {
        visible: recentFilesModel.count > 0
    }

    MenuItem {
        text: "Clear menu"
        enabled: recentFilesModel.count > 0
        onTriggered: recentFilesModel.clear()
    }

Note that in this case, the index parameter passed to insertItem() is relative to the position of the Instantiator in the menu, as opposed to absolute position in the menu.

See also MenuItem and MenuSeparator.


style : Component


title : string

Title for the menu as a submenu or in a menubar.

Mnemonics are supported by prefixing the shortcut letter with &. For instance, "\&File" will bind the Alt-F shortcut to the "File" menu. Note that not all platforms support mnemonics.

Its value defaults to the empty string.


type : enumeration

This property is read-only and constant, and its value is MenuItemType.Menu.


visible : bool

Whether the menu should be visible. This is only enabled when the menu is used as a submenu or in the menubar. Its value defaults to true.


Method Documentation

MenuItem addItem(string text)

Adds an item to the menu. Returns the newly created MenuItem.

See also insertItem().


Menu addMenu(string title)

Adds a submenu to the menu. Returns the newly created Menu.

See also insertMenu().


void addSeparator()

Adds a separator to the menu.

See also insertSeparator().


void insertItem(int before, object item)

Inserts the item at the index before in the current menu. In this case, item can be either a MenuItem, a MenuSeparator, or a Menu.

See also removeItem().


MenuItem insertItem(int before, string title)

Creates and inserts an item with title title at the index before in the current menu. Returns the newly created MenuItem.

See also addItem().


MenuItem insertMenu(int before, string title)

Creates and inserts a submenu with title title at the index before in the current menu. Returns the newly created Menu.

See also addMenu().


void insertSeparator(int before)

Creates and inserts a separator at the index before in the current menu.

See also addSeparator().


Opens this menu under the mouse cursor. It can block on some platforms, so test it accordingly.


void removeItem(item)

Removes the item from the menu. In this case, item can be either a MenuItem, a MenuSeparator, or a Menu.

See also insertItem().


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