Menu QML Type▲
-
Import Statement: import Qt.labs.platform
-
Inherits: QtObject
Detailed Description▲
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()
}
}
Submenus▲
Menu
{
title
:
qsTr("Edit"
)
Menu
{
title
:
qsTr("Advanced"
)
MenuItem
{
text
:
qsTr("Auto-indent Selection"
)
onTriggered
:
autoIndentSelection()
}
MenuItem
{
text
:
qsTr("Rewrap Paragraph"
)
onTriggered
:
rewrapParagraph()
}
}
}
Dynamically generating menu items▲
Menu
{
title
:
qsTr("File"
)
Menu
{
id
:
recentFilesMenu
title
:
qsTr("Recent Files"
)
enabled
:
recentFilesInstantiator.count &
gt; 0
Instantiator
{
id
:
recentFilesInstantiator
model
:
settings.recentFiles
delegate
:
MenuItem
{
text
:
settings.displayableFilePath(modelData)
onTriggered
:
loadFile(modelData)
}
onObjectAdded
:
(index
, object) =&
gt; recentFilesMenu.insertItem(index
, object)
onObjectRemoved
:
(index
, object) =&
gt; recentFilesMenu.removeItem(object)
}
MenuSeparator
{}
MenuItem
{
text
:
qsTr("Clear Recent Files"
)
onTriggered
:
settings.clearRecentFiles()
}
}
}
Availability▲
A native platform menu is currently available on the following platforms:
-
macOS
-
iOS
-
Android
-
Linux (only available as a stand-alone context menu when running with the GTK+ platform theme)
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 instead of QGuiApplication.
To link against the 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 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.
See Also▲
See also MenuItem, MenuSeparator, MenuBar
Property Documentation▲
[default] data : list<QtObject>▲
This default property holds the list of all objects declared as children of the menu. The data property includes objects that are not MenuItem instances, such as Timer and QtObject.
See Also▲
See also items
enabled : bool▲
This property holds whether the menu is enabled. The default value is true.
font : font▲
icon group▲
[since Qt.labs.platform 1.1 (Qt 5.12)] icon.mask : bool
[since Qt.labs.platform 1.1 (Qt 5.12)] icon.name : string
[since Qt.labs.platform 1.1 (Qt 5.12)] icon.source : url
This property holds the menu item's icon.
This QML property was introduced in Qt.labs.platform 1.1 (Qt 5.12).
items : list<MenuItem>▲
This property holds the list of items in the menu.
[read-only] menuBar : MenuBar▲
[read-only] menuItem : MenuItem▲
minimumWidth : int▲
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.
title : string▲
This property holds the menu's title.
type : enumeration▲
This property holds the type of the menu.
Available values:
Constant |
Description |
---|---|
Menu.DefaultMenu |
A normal menu (default). |
Menu.EditMenu |
An edit menu with pre-populated cut, copy and paste items. |
visible : bool▲
This property holds whether the menu is visible. The default value is true.
Signal Documentation▲
aboutToHide()▲
This signal is emitted when the menu is about to be hidden from the user.
The corresponding handler is onAboutToHide.
aboutToShow()▲
This signal is emitted when the menu is about to be shown to the user.
The corresponding handler is onAboutToShow.
Method Documentation▲
void addItem(MenuItem item)▲
Adds an item to the end of the menu.
void addMenu(Menu submenu)▲
Adds a submenu to the end of the menu.
void clear()▲
Removes all items from the menu.
void close()▲
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)▲
Inserts a submenu at the specified index in the menu.
void open(MenuItem item)▲
Opens the menu at the current mouse position, optionally aligned to a menu item.
void open(Item target, MenuItem item)▲
Opens the menu at the specified target item, 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.