Menu QML Type | Qt Labs Platform 5.15.18 (original) (raw)

A native menu. More...

Import Statement: import Qt.labs.platform 1.1
Since: Qt 5.8
Inherits: QtObject

Properties

Signals

Methods

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()
}

}

To create submenus, declare a Menu as a child of another Menu:

Menu { title: qsTr("Edit")

[Menu](qml-qt-labs-platform-menu.html) {
    title: qsTr("Advanced")

    [MenuItem](qml-qt-labs-platform-menuitem.html) {
        text: qsTr("Auto-indent Selection")
        onTriggered: autoIndentSelection()
    }

    [MenuItem](qml-qt-labs-platform-menuitem.html) {
        text: qsTr("Rewrap Paragraph")
        onTriggered: rewrapParagraph()
    }
}

}

Dynamically Generating Menu Items

It is possible to dynamically generate menu items. One of the easiest ways to do so is with 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:

Menu { title: qsTr("File")

[Menu](qml-qt-labs-platform-menu.html) {
    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](qml-qt-labs-platform-menuseparator.html) {}

    [MenuItem](qml-qt-labs-platform-menuitem.html) {
        text: qsTr("Clear Recent Files")
        onTriggered: settings.clearRecentFiles()
    }
}

}

Availability

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 instead of QGuiApplication.

To link against the QtWidgets library, add the following to your qmake project file:

Create an instance of QApplication in main():

#include #include

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(); }

Note: Types in Qt.labs modules are not guaranteed to remain compatible in future versions.

See also MenuItem, MenuSeparator, and MenuBar.

Property Documentation

[default] data : list<Object>

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 items.

This property holds whether the menu is enabled. The default value is true.

This property holds the menu's font.

See also text.

This property holds the menu item's icon.

This QML property was introduced in Qt.labs.platform 1.1 (Qt 5.12).

This property holds the list of items in the menu.

This property holds the menubar that the menu belongs to, or null if the menu is not in a menubar.

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.

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:

Constant Description
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.

Signal Documentation

This signal is emitted when the menu is about to be hidden from the user.

Note: The corresponding handler is onAboutToHide.

This signal is emitted when the menu is about to be shown to the user.

Note: The corresponding handler is onAboutToShow.

Method Documentation

Adds an item to the end of the menu.

void addMenu(Menu submenu)

Adds a submenu to the end of the menu.

Removes all items from the menu.

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.

Opens the menu at the specified target item, optionally aligned to a menu item.

Opens the menu at the current mouse position, optionally aligned to a menu item.

Removes an item from the menu.

void removeMenu(Menu submenu)

Removes a submenu from the menu.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.