zl程序教程

您现在的位置是:首页 >  其它

当前栏目

第45篇 QML控件类型 之 Menu(菜单)

类型 控件 菜单 45 qml Menu
2023-09-14 09:05:33 时间

1、Menu

(1)点击左右键弹出菜单:

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

Window {
    id: root
    visible: true
    width: 640
    height: 480

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
            if (mouse.button === Qt.RightButton)
            {
                contextMenu.popup()
            }
            else if(mouse.button === Qt.LeftButton)
            {
                leftContextMenu.popup()
            }
        }

        Menu {
            id: contextMenu
            MenuItem { text: "Cut"  }
            MenuItem { text: "Copy" }
            MenuItem { text: "Paste" }
        }

        Menu {
            id: leftContextMenu
            MenuItem { text: "fjf" }
            MenuItem { text: "cjs" }
        }
    }
}