MenuItem QML Type
在菜单中显示一个项目。更多...
| Import Statement: | import QtQuick.Controls |
| Inherits: |
属性
- arrow : Item
(since QtQuick.Controls 2.3 (Qt 5.10)) - highlighted : bool
- implicitTextPadding : real
(since 6.8) - menu : Menu
(since QtQuick.Controls 2.3 (Qt 5.10)) - subMenu : Menu
(since QtQuick.Controls 2.3 (Qt 5.10)) - textPadding : real
(since 6.8)
信号
- void triggered()
详细描述
MenuItem 是一种便捷类型,它实现了AbstractButton 接口,提供了一种熟悉的方式来响应菜单项被triggered 等操作。
MenuItem 继承了AbstractButton 的 API。例如,您可以使用AbstractButton API 设置text 和图标。
Button {
id: fileButton
text: "File"
onClicked: menu.open()
Menu {
id: menu
MenuItem {
text: "New..."
onTriggered: document.reset()
}
MenuItem {
text: "Open..."
onTriggered: openDialog.open()
}
MenuItem {
text: "Save"
onTriggered: saveDialog.open()
}
}
}属性文档
arrow : Item [since QtQuick.Controls 2.3 (Qt 5.10)]
该属性用于存储子菜单箭头项。
该属性在 QtQuick.Controls 2.3(Qt 5.10)中引入。
另请参阅 “自定义菜单”。
highlighted : bool
该属性用于表示菜单项是否被用户高亮显示。
菜单项可通过鼠标悬停或键盘导航被高亮显示。
默认值为false 。
另请参阅 Menu::currentIndex 。
implicitTextPadding : real [since 6.8]
该属性存储从contentItem 的左边缘到文本所需的最小间距。它用于计算Menu 内所有MenuItems的textPadding 。
该属性在 Qt 6.8 中引入。
另请参阅 textPadding 。
menu : Menu [read-only, since QtQuick.Controls 2.3 (Qt 5.10)]
该属性存储包含此菜单项的菜单,如果该菜单项不在任何菜单中,则返回null 。
该属性于 QtQuick.Controls 2.3(Qt 5.10)中引入。
subMenu : Menu [read-only, since QtQuick.Controls 2.3 (Qt 5.10)]
该属性存储此项目在父菜单中显示的子菜单,如果此项目不是子菜单项,则该属性值为null 。
该属性首次出现在 QtQuick.Controls 2.3(Qt 5.10)中。
textPadding : real [read-only, since 6.8]
该属性存储同一menu 内所有菜单项中找到的最大implicitTextPadding 值。
样式可以使用此属性,以确保同一菜单(Menu)内的所有菜单项最终都能根据text 进行对齐。
一个Menu 可以包含许多不同的MenuItems,有些可以被选中,有些可以带有图标,有些则仅包含文本。 而且,样式通常希望确保所有MenuItem 内的文本最终都左对齐(对于mirrored 项,则右对齐)。通过让每个 将自身所需的最小文本内边距分配给implicitTextPadding (同时考虑图标和复选标记),但使用textPadding来实际定位text ,所有MenuItems最终都应保持对齐
为了使此方案生效,所有 MenuItems 都应将 `implicitTextPadding ` 设置为从contentItem 的左边缘到文本所需的最小间距。
该属性在 Qt 6.8 中引入。
另请参阅 implicitTextPadding 。
信号文档
void triggered()
当用户触发该菜单项时,会发出此信号。
注意: 相应的处理程序 为onTriggered 。
© 2026 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.