A native menu. 更多...
| import 语句: | import Qt.labs.platform 1.0 |
| Since: | Qt 5.8 |
| 继承: | QtObject |
The Menu type provides a QML API for native platform menu popups.
Menu can be used in a MenuBar , or as a stand-alone context menu. The following example shows how to open a context menu on right mouse click:
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
onClicked: zoomMenu.open()
}
Menu {
id: zoomMenu
MenuItem {
text: qsTr("Zoom In")
shortcut: StandardKey.ZoomIn
onTriggered: zoomIn()
}
MenuItem {
text: qsTr("Zoom Out")
shortcut: StandardKey.ZoomOut
onTriggered: zoomOut()
}
}
A native platform menu is currently available on the following platforms:
The Qt Labs Platform module uses Qt Widgets as a fallback on platforms that do not have a native implementation available. Therefore, applications that use types from the Qt Labs Platform module should link to QtWidgets and use QApplication 而不是 QGuiApplication .
要链接到 QtWidgets library, add the following to your qmake project file:
QT += widgets
Create an instance of
QApplication
in
main()
:
#include <QApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
注意: Types in Qt.labs modules are not guaranteed to remain compatible in future versions.
另请参阅 MenuItem , MenuSeparator ,和 MenuBar .
This default property holds the list of all objects declared as children of the menu. The data property includes objects that are not MenuItem 实例,譬如 Timer and QtObject .
另请参阅 项 .
This property holds whether the menu is enabled. The default value is
true
.
|
font : font |
This property holds the menu's font.
另请参阅 text .
This property holds the theme name of the menu's icon.
另请参阅 iconSource and QIcon::fromTheme() .
|
iconSource : url |
This property holds the url of the menu's icon.
另请参阅 iconName .
|
项 : list < MenuItem > |
This property holds the list of items in the menu.
|
[read-only] menuBar : MenuBar |
This property holds the menubar that the menu belongs to, or
null
if the menu is not in a menubar.
|
[read-only] menuItem : MenuItem |
This property holds the item that presents the menu (in a parent menu).
This property holds the minimum width of the menu. The default value is
-1
(no minimum width).
|
[read-only] parentMenu : Menu |
This property holds the parent menu that the menu belongs to, or
null
if the menu is not a sub-menu.
|
[read-only] systemTrayIcon : SystemTrayIcon |
This property holds the system tray icon that the menu belongs to, or
null
if the menu is not in a system tray icon.
This property holds the menu's title.
This property holds the type of the menu.
Available values:
| 常量 | 描述 |
|---|---|
Menu.DefaultMenu
|
A normal menu (default). |
Menu.EditMenu
|
An edit menu with pre-populated cut, copy and paste items. |
This property holds whether the menu is visible. The default value is
true
.
This signal is emitted when the menu is about to be hidden from the user.
This signal is emitted when the menu is about to be shown to the user.
|
void addItem ( MenuItem item ) |
添加 item to the end of the menu.
|
void addMenu ( Menu submenu ) |
添加 submenu to the end of the menu.
Removes all items from the menu.
Closes the menu.
|
void insertItem ( int index , MenuItem item ) |
Inserts an item at the specified index in the menu.
|
void insertMenu ( int index , Menu submenu ) |
插入 submenu at the specified index in the menu.
Opens the menu at the specified target item, optionally aligned to a menu item .
|
void open ( MenuItem item ) |
Opens the menu at the current mouse position, optionally aligned to a menu item .
|
void removeItem ( MenuItem item ) |
Removes an item from the menu.
|
void removeMenu ( Menu submenu ) |
Removes a submenu from the menu.