Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
6.5.0
-
None
Description
The QtQuick.Controls Menu component is essentially a ListView without support for any of the dynamic instantiation, pooling or caching that ListView supports. Most Menu implementations actually use a ListView internally to show their items but because the Menu contentModel requires the items to be created upfront Menus with many items incur a significant upfront compilation penalty.
Take for example the Instantiator example found in the docs: https://doc.qt.io/qt-6/qml-qtquick-controls2-menu.html#using-instantiator
If the Instantiator's model is some list model with many items then all of the MenuItem instances will be created up front regardless of whether they are actually visible in the ListView.
One way around this is to create a custom Menu with a Popup and a ListView but that doesn't work if you want to show the Menu in a MenuBar for example.
See the attached Main.qml for an example of this issue. With 1000 menu items the menu takes a few seconds to open.
QtControls.Menu { id: menu title: qsTr("Recent Files") Instantiator { active: menu.visible // Some large model model: { let array = [] for (let i = 0; i < 1000; i++) { array.push(i + "") } return array } delegate: QtControls.MenuItem { text: modelData } onObjectAdded: (index, object) => menu.insertItem(index, object) onObjectRemoved: (index, object) => menu.removeItem(object) } }