Qt World Summit: Submit your Presentation

QML Menu

  • After reading through both the documentation for Menu and the related source code (links listed below), I can't figure out a way to use use the Menu for standalone use. The documentation states it can be used "as a standalone or context menu". It's clear how it can be used as a context menu and in the menubar, but the source code seems to focus on popping the menu up at the mouse cursor. Am I missing something? Is there an easy way to use the Menu element for a standalone menu without jumping through error prone hacking of the internals or recreating the functionality with a custom component? Any and all help is appreciated. Thanks.



  • To us it in a menu bar like for instance ApplicationWindow it is just
    @ menuBar: MenuBar {
    Menu {
    title: qsTr("File")
    MenuItem {
    text: qsTr("Exit")
    onTriggered: Qt.quit();

    To use it as a context menu do
    @Menu {id: contextMenu ..... }
    MouseArea {
    anchors.fill: thing you want to respond to clicks
    onClicked: {
    if (mouse.button === Qt.RightButton) {

    If the whole app locks up try passing -dograb to your application. I found in debug mode it disables grabbing (for some unknown reason).

  • I appreciate the response, but I'm not quite sure it addresses my question. Both of those are well documented and make perfect sense. It's using the menu for a standalone menu, which the documentation claims is one of the potential uses, that I'm not clear on. Thanks.

  • Moderators


    In the documentation, "standalone menu" = "context menu" = "popup menu" = not-bound-to-the-menubar menu. (Personally, I wouldn't use the word "standalone" here)

    The other 2 menu types are bound to the menu bar either directly ("top-level menu") or indirectly ("submenu"). The Qt Quick Controls Menu doesn't provide other kinds of menus; the first sentence in the documentation says, "[The QML Menu type] provides a menu component for use in menu bars, as context menu, and other popup menus."

    I believe you're looking for something that lets you implement a main menu or sidebar menu? For this use case, create a row/column/grid of "Buttons":http://qt-project.org/doc/qt-5.1/qtquickcontrols/qml-qtquick-controls1-button.html or "Rectangles":http://qt-project.org/doc/qt-5.1/qtquick/qml-qtquick2-rectangle.html

  • Thanks JKSH. I suspected that might be the case after reading through the code. It's unfortunate since there is a lot of overlapping code (keyboard handling, stack / visibility management, sub menus, etc) that could be useful for normal standalone menus. That said, it's not too hard to create a custom one. I appreciate the help.

  • Moderators

    You're welcome :)

    [quote author="webyte" date="1381124811"]It's unfortunate since there is a lot of overlapping code (keyboard handling, stack / visibility management, sub menus, etc) that could be useful for normal standalone menus.[/quote]Yes, it's true that there's lots of reusable code here.

    A bit of history will probably provide insight to why the components are designed this way: The Qt Quick Controls module was created because users wanted a way to create classic desktop style applications (think Microsoft Office 2003 or LibreOffice, and their config dialogs). These classic GUIs didn't use "standalone" menus, so Qt Quick Controls doesn't provide them either.

    In any case, games (and more recently, websites and phone apps) typically implement vastly different styles of standalone menus, each with their own unique look-and-feel, animations, behaviours, etc. I'd imagine that it would be hard to create a menu component that can be satisfactorily customized to match the multitude of styles out there.

Log in to reply