aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndreas Eliasson <andreas.eliasson@qt.io>2022-12-09 13:00:20 +0100
committerAndreas Eliasson <andreas.eliasson@qt.io>2022-12-21 16:49:25 +0100
commit239082b5ad44adfe2711cd334da1a9d57dd79a6f (patch)
treeb6529ea1a3155888eecaa60d2ec4fd9de82e2ee9 /src
parent21455028f34bdb09981c7f4a0537356857f3ca75 (diff)
Doc: Add createObject() approach to dynamically generating menu items
Fixes: QTBUG-108798 Pick-to: 6.5 6.4 6.2 Change-Id: Ie8c8edfe00b11eca0082e89c864ec20cef8748e5 Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/quickcontrols/doc/snippets/qtquickcontrols-menu-createObject.qml37
-rw-r--r--src/quicktemplates/qquickmenu.cpp25
2 files changed, 57 insertions, 5 deletions
diff --git a/src/quickcontrols/doc/snippets/qtquickcontrols-menu-createObject.qml b/src/quickcontrols/doc/snippets/qtquickcontrols-menu-createObject.qml
new file mode 100644
index 0000000000..31991ced70
--- /dev/null
+++ b/src/quickcontrols/doc/snippets/qtquickcontrols-menu-createObject.qml
@@ -0,0 +1,37 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
+
+import QtQuick
+import QtQuick.Controls
+
+//! [createObject]
+Row {
+ anchors.centerIn: parent
+
+ Component {
+ id: menuItemComponent
+
+ MenuItem {}
+ }
+
+ Button {
+ id: button
+ text: "Menu"
+ onClicked: menu.open()
+ Menu {
+ id: menu
+ }
+ }
+
+ Button {
+ text: "Add item"
+ onClicked: {
+ onClicked: {
+ let menuItem = menuItemComponent.createObject(
+ menu.contentItem, { text: qsTr("New item") })
+ menu.addMenu(menuItem)
+ }
+ }
+ }
+}
+//! [createObject]
diff --git a/src/quicktemplates/qquickmenu.cpp b/src/quicktemplates/qquickmenu.cpp
index 0324490a1b..ea37af1885 100644
--- a/src/quicktemplates/qquickmenu.cpp
+++ b/src/quicktemplates/qquickmenu.cpp
@@ -148,15 +148,30 @@ static const int SUBMENU_DELAY = 225;
menu to go outside of the window (to animate it moving into view, for
example), set the margins property to \c -1.
- \section1 Dynamically generating menu items
+ \section1 Dynamically Generating Menu Items
- You can dynamically generate menu items with \l Instantiator. The following
- code shows how you can implement a "Recent Files" submenu, where the items
- come from a list of files stored in settings:
+ You can dynamically create menu items with \l Instantiator or
+ \l {Dynamic QML Object Creation from JavaScript} {dynamic object creation}.
+
+ \section2 Using Instantiator
+
+ You can dynamically generate menu items with \l Instantiator. The
+ following code shows how you can implement a "Recent Files" submenu,
+ where the items come from a list of files stored in settings:
\snippet qtquickcontrols-menu-instantiator.qml menu
- \sa {Customizing Menu}, MenuItem, {Menu Controls}, {Popup Controls}
+ \section2 Using Dynamic Object Creation
+
+ You can also dynamically load a component from a QML file using
+ \l {QtQml::Qt::createComponent()} {Qt.createComponent()}. Once the component
+ is ready, you can call its \l {Component::createObject()} {createObject()}
+ method to create an instance of that component.
+
+ \snippet qtquickcontrols-menu-createObject.qml createObject
+
+ \sa {Customizing Menu}, MenuItem, {Menu Controls}, {Popup Controls},
+ {Dynamic QML Object Creation from JavaScript}
*/
/*!