From e4901286c801162d6b9b94b10d23dafca73c5068 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Tue, 6 Mar 2018 12:15:11 +0100 Subject: Doc: add an example of submenus and dynamically generated menu items Task-number: QTBUG-66874 Change-Id: I1e34039e4fa0344b1efdcb2977586d97ada6b31e Reviewed-by: J-P Nurmi --- src/imports/platform/qquickplatformmenu.cpp | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src') diff --git a/src/imports/platform/qquickplatformmenu.cpp b/src/imports/platform/qquickplatformmenu.cpp index ec5c4804..6168938c 100644 --- a/src/imports/platform/qquickplatformmenu.cpp +++ b/src/imports/platform/qquickplatformmenu.cpp @@ -100,6 +100,68 @@ QT_BEGIN_NAMESPACE } \endcode + \section2 Submenus + + To create submenus, declare a Menu as a child of another Menu: + + \qml + Menu { + title: qsTr("Edit") + + Menu { + title: qsTr("Advanced") + + MenuItem { + text: qsTr("Auto-indent Selection") + onTriggered: autoIndentSelection() + } + + MenuItem { + text: qsTr("Rewrap Paragraph") + onTriggered: rewrapParagraph() + } + } + } + \endqml + + \section2 Dynamically Generating Menu Items + + It is possible to dynamically generate menu items. One of the easiest ways + to do so is with \l Instantiator. For example, to implement a + "Recent Files" submenu, where the items are based on a list of files stored + in settings, the following code could be used: + + \qml + Menu { + title: qsTr("File") + + Menu { + id: recentFilesSubMenu + title: qsTr("Recent Files") + enabled: recentFilesInstantiator.count > 0 + + Instantiator { + id: recentFilesInstantiator + model: settings.recentFiles + delegate: MenuItem { + text: settings.displayableFilePath(modelData) + onTriggered: loadFile(modelData) + } + + onObjectAdded: recentFilesSubMenu.insertItem(index, object) + onObjectRemoved: recentFilesSubMenu.removeItem(object) + } + + MenuSeparator {} + + MenuItem { + text: qsTr("Clear Recent Files") + onTriggered: settings.clearRecentFiles() + } + } + } + \endqml + \section2 Availability A native platform menu is currently available on the following platforms: -- cgit v1.2.3