QML添加右键菜单

MouseArea {
        id: mouseRegion
        anchors.fill: parent;
        acceptedButtons: Qt.LeftButton | Qt.RightButton // 激活右键(别落下这个)
 
        onClicked: {
            if (mouse.button == Qt.RightButton) { // 右键菜单
                //
                contentMenu.popup()
            }
        }
    }
 
    Menu { // 右键菜单
        //title: "Edit"
        id: contentMenu
 
        MenuItem {
            text: "Cut"
            shortcut: "Ctrl+X"
            onTriggered: {}
        }
 
        MenuItem {
            text: "Copy"
            shortcut: "Ctrl+C"
            onTriggered: {}
        }
 
        MenuItem {
            text: "Paste"
            shortcut: "Ctrl+V"
            onTriggered: {}
        }
 
        MenuSeparator { }
 
        Menu {
            title: "More Stuff"
 
            MenuItem {
                text: "Do Nothing"
            }
        }
    }
效果图
原文地址:https://www.cnblogs.com/-Donny/p/5109948.html