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"
            }
        }
    }

 

效果图
QML添加右键菜单_第1张图片

转载于:https://www.cnblogs.com/-Donny/p/5109948.html

你可能感兴趣的:(QML添加右键菜单)