Skip to content

使用非可视的 ContextMenuArea 元素来声明用户可以显示上下文菜单的区域。

当用户在 ContextMenuArea 元素覆盖的区域内右键单击时, 或者当 ContextMenuArea 内具有焦点的 FocusScope 拥有焦点时用户按下键盘上的 "Menu" 键时, 将显示上下文菜单。在 Android 上,通过长按显示菜单。 调用 ContextMenuArea 元素上的 show() 函数可通过代码显示上下文菜单。

ContextMenuArea 的子元素之一必须是 Menu 元素,它定义了要显示的菜单。 最多只能有一个 Menu 子元素,所有其他子元素必须是不同类型,并将作为常规可视子元素显示。 通过在该 Menu 内放置 MenuItemMenu 元素来定义菜单的结构。

函数

show(Point)

调用此函数以在相对于 ContextMenuArea 元素的给定位置通过代码显示上下文菜单。

close()

如果上下文菜单当前处于打开状态,则将其关闭。

enabled

bool default: true

禁用时,Menu 不显示。

Menu 元素放置在 MenuBarContextMenuArea 或另一个 Menu 内。 使用 MenuItem 子元素作为各个菜单项,使用 Menu 子元素创建子菜单,使用 MenuSeparator 创建分隔符。

title

string default: ""

这是在菜单栏或父菜单中显示的菜单标签。

enabled

bool default: true

禁用时,Menu 可以被选中但不能被激活。

icon

image default: the empty image

在父菜单中时显示在标题旁的图标。

MenuItem 表示单个菜单项。它必须是 Menu 元素的子元素。

title

string default: ""

为该菜单项显示的标题。

enabled

bool default: true

禁用时,MenuItem 可以被选中但不能被激活。

checkable

bool default: false

当为 true 时,MenuItem 可以被选中。当用户激活该菜单项时,checked 属性的值将被切换。

checked

bool (in-out) default: false

当为 true 时,将在 MenuItem 的标题旁边显示一个勾选标记。

icon

image default: the empty image

显示在标题旁的图标。

shortcut

keys default: @keys()

MenuItem 的键盘快捷键。

此属性只能在属于 MenuBarMenuItem 中设置。

activated()

当菜单项被激活时调用。

MenuSeparator 表示菜单中的分隔符。 它不能有子元素,并且没有属性或回调。 位于菜单开头或结尾的 MenuSeparator 将不可见。 连续的 MenuSeparator 将被合并为一个。

示例

slint
export component Example {    ContextMenuArea {        Menu {            MenuItem {                title: @tr("Cut");                activated => { debug("Cut"); }            }            MenuItem {                title: @tr("Copy");                activated => { debug("Copy"); }            }            MenuItem {                title: @tr("Paste");                activated => { debug("Paste"); }            }            MenuSeparator {}            Menu {                title: @tr("Find");                MenuItem {                    title: @tr("Find Next");                }                MenuItem {                    title: @tr("Find Previous");                }            }        }    }}

基于 MIT 协议发布