Menu without Menuitem



  • I have a menubar with some menus on it. My problem is that some of the menus should have no items. So just by clicking on the menu name an action should be performed or a function called or something like this.

    MenuBar {
            Menu {
                title: qsTr("File")
    
                MenuItem { text: qsTr("Open") }
                MenuItem { text: qsTr("Save") }
                MenuSeparator { }
                MenuItem { text: qsTr("Close") }
            }
            
            Menu {
                title: qsTr("Tools");
            }
    }
    

    Like in the sample above, if the user clicks on the Menu 'Tools' there should be an action. As far as i can see there is no 'onclick' or 'onTriggered' handler for the Menu, just for MenuItem.

    Does anyone have an idea?



  • Has no one an idea how to add a single clickable item to a menubar?



  • @PhTe You could use the signal handleronAboutToShow or even onPopupVisibleChanged

    Example:

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    ApplicationWindow {
        visible: true
    
        menuBar:MenuBar {
            Menu {
                title: qsTr("File")
    
                MenuItem { text: qsTr("Open") }
                MenuItem { text: qsTr("Save") }
                MenuSeparator { }
                MenuItem { text: qsTr("Close") }
            }
    
            Menu {
                id: emptyMenu
                title: qsTr("Tools");
    
                onPopupVisibleChanged: console.log("emptyMenu - onPopupVisibleChanged")
    
                onAboutToShow: {
                    console.log("emptyMenu - onAboutToShow")
                }
            }
        }
    
        Menu { 
            id: contextMenu
            MenuItem {
                text: qsTr('Delete')
            }
    
            onAboutToShow: {
                console.log("contextMenu - onAboutToShow")
            }
        }
    
        MainForm {
            anchors.fill: parent
            mouseArea.onClicked: {
                contextMenu.popup()
            }
        }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.