QML controls 2 context menu



  • Hi,
    I'm trying to show a simple context menu for an item:

    Item {
        // bunch of children
    
        MouseArea {
                anchors.fill: parent
                onClicked: {
                    if (mouse.button === Qt.RightButton) {
                        contextMenu.x = mouse.x;
                        contextMenu.y = mouse.y;
    
                        contextMenu.open();
                    }
                }
            }
    
        Menu {
            id: contextMenu
    
            MenuItem {
                text: 'text'
            }
        }
    }
    

    Unfortunately, nothing is shown after the right-click. The documentation for Menu is really lacking any kind of info. Debug shows the handler for mouse click is working. What could be wrong?



  • Hi! See the two comments inside the code:

        Item {
            anchors.fill: parent // make sure mouse area's parent is bigger than zero
    
            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton | Qt.RightButton // default is Qt.LeftButton only
                onClicked: {
                    if (mouse.button === Qt.RightButton) {
                        contextMenu.x = mouse.x;
                        contextMenu.y = mouse.y;
                        contextMenu.open()
                    }
                }
            }
    
            Menu {
                id: contextMenu
                MenuItem {
                    text: 'text'
                }
            }
        }
    


  • @Wieland said in QML controls 2 context menu:

    acceptedButtons: Qt.LeftButton | Qt.RightButton

    Thanks - setting acceptedButton solved the problem.


Log in to reply
 

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