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

When used as a context menu, the recommended way of opening the menu is to call popup() . Unless a position is explicitly specified, the menu is positioned at the mouse cursor on desktop platforms that have a mouse cursor available, and otherwise centered over its parent item.

MouseArea {
    anchors.fill: parent
    acceptedButtons: Qt.LeftButton | Qt.RightButton
    onClicked: {
        if (mouse.button === Qt.RightButton)
            contextMenu.popup()
    }
    onPressAndHold: {
        if (mouse.source === Qt.MouseEventNotSynthesized)
            contextMenu.popup()
    }
    Menu {
        id: contextMenu
        MenuItem { text: "Cut" }
        MenuItem { text: "Copy" }
        MenuItem { text: "Paste" }
    }
}
					

When used as a popup menu, it is easiest to specify the position by specifying the desired x and y coordinates using the respective properties, and call open() to open the menu.

Button {
    id: fileButton
    text: "File"
    onClicked: menu.open()
    Menu {
        id: menu
        y: fileButton.height
        MenuItem {
            text: "New..."
        }
        MenuItem {
            text: "Open..."
        }
        MenuItem {
            text: "Save"
        }
    }
}
					

由于 QtQuick .Controls 2.3 (Qt 5.10), it is also possible to create sub-menus and declare Action objects inside Menu:

Menu {
    Action { text: "Cut" }
    Action { text: "Copy" }
    Action { text: "Paste" }
    MenuSeparator { }
    Menu {
        title: "Find/Replace"
        Action { text: "Find Next" }
        Action { text: "Find Previous" }
        Action { text: "Replace" }
    }
}
					

Sub-menus are cascading by default on desktop platforms that have a mouse cursor available. Non-cascading menus are shown one menu at a time, and centered over the parent menu.

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.

Margins

As it is inherited from Popup, Menu supports margins . By default, all of the built-in styles specify 0 for Menu's margins to ensure that the menu is kept within the bounds of the window. To allow a menu to go outside of the window (to animate it moving into view, for example), set the margins property to -1 .

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

特性文档编制

[since QtQuick.Controls 2.3 (Qt 5.10)] cascade : bool

This property holds whether the menu cascades its sub-menus.

The default value is platform-specific. Menus are cascading by default on desktop platforms that have a mouse cursor available. Non-cascading menus are shown one menu at a time, and centered over the parent menu.

注意: Changing the value of the property has no effect while the menu is open.

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

另请参阅 overlap .

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

[read-only, since QtQuick.Controls 2.3 (Qt 5.10)] count : int

This property holds the number of items.

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

[since QtQuick.Controls 2.3 (Qt 5.10)] currentIndex : int

This property holds the index of the currently highlighted item.

Menu items can be highlighted by mouse hover or keyboard navigation.

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

另请参阅 MenuItem::highlighted .

[since QtQuick.Controls 2.3 (Qt 5.10)] delegate : Component

This property holds the component that is used to create items to present actions.

Menu {
    Action { text: "Cut" }
    Action { text: "Copy" }
    Action { text: "Paste" }
}
					

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

另请参阅 Action .

focus : bool

This property holds whether the popup wants focus.

When the popup actually receives focus, activeFocus 将是 true 。更多信息,见 Qt Quick 中的键盘聚焦 .

默认值为 false .

另请参阅 activeFocus .

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

This property holds the amount of pixels by which the menu horizontally overlaps its parent menu.

The property only has effect when the menu is used as a cascading sub-menu.

The default value is style-specific.

注意: Changing the value of the property has no effect while the menu is open.

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

另请参阅 cascade .

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.


方法文档编制

Opens the menu at the specified position x , y in the popups coordinate system, that is, a coordinate relative to its parent 项。

The menu can be optionally aligned to a specific menu item .

This QML method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

另请参阅 dismiss() and Popup::open() .

Opens the menu at the specified position pos in the popups coordinate system, that is, a coordinate relative to its parent 项。

The menu can be optionally aligned to a specific menu item .

This QML method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

另请参阅 Popup::open() .

Opens the menu at the mouse cursor on desktop platforms that have a mouse cursor available, and otherwise centers the menu over its parent 项。

The menu can be optionally aligned to a specific menu item .

This QML method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

另请参阅 Popup::open() .

[since QtQuick.Controls 2.3 (Qt 5.10)] Action actionAt ( int index )

Returns the action at index ,或 null if the index is not valid or there is no action at the specified index.

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

[since QtQuick.Controls 2.3 (Qt 5.10)] void addAction ( Action action )

添加 action to the end of this menu.

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

void addItem ( Item item )

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

[since QtQuick.Controls 2.3 (Qt 5.10)] void addMenu ( Menu menu )

添加 menu as a sub-menu to the end of this menu.

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

[since QtQuick.Controls 2.3 (Qt 5.10)] void dismiss ()

Closes all menus in the hierarchy that this menu belongs to.

注意: 不像 close() that only closes a menu and its sub-menus, dismiss() closes the whole hierarchy of menus, including the parent menus. In practice, close() is suitable e.g. for implementing navigation in a hierarchy of menus, and dismiss() is the appropriate method for closing the whole hierarchy of menus.

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

另请参阅 popup() and Popup::close() .

[since QtQuick.Controls 2.3 (Qt 5.10)] void insertAction ( int index , Action action )

插入 action at index . The index is within all items in the menu.

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

void insertItem ( int index , Item item )

插入 item at index .

[since QtQuick.Controls 2.3 (Qt 5.10)] void insertMenu ( int index , Menu menu )

插入 menu as a sub-menu at index . The index is within all items in the menu.

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

Item itemAt ( int index )

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

Returns the sub-menu at index ,或 null if the index is not valid or there is no sub-menu at the specified index.

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

void moveItem ( int from , int to )

Moves an item from one index to another.

[since QtQuick.Controls 2.3 (Qt 5.10)] void removeAction ( Action action )

Removes and destroys the specified action .

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

[since QtQuick.Controls 2.3 (Qt 5.10)] void removeItem ( Item item )

Removes and destroys the specified item .

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

[since QtQuick.Controls 2.3 (Qt 5.10)] void removeMenu ( Menu menu )

Removes and destroys the specified menu .

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

[since QtQuick.Controls 2.3 (Qt 5.10)] Action takeAction ( int index )

Removes and returns the action at index . The index is within all items in the menu.

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

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

[since QtQuick.Controls 2.3 (Qt 5.10)] MenuItem takeItem ( int index )

Removes and returns the item at index .

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

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).

[since QtQuick.Controls 2.3 (Qt 5.10)] Menu takeMenu ( int index )

Removes and returns the menu at index . The index is within all items in the menu.

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

This method was introduced in QtQuick.Controls 2.3 (Qt 5.10).