Qml:鼠标事件

简介: Qml:鼠标事件
import QtQuick
import QtQuick.Window
Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Test Mouse")
    Rectangle
    {
        width: 100
        height: 100
        color:"green"
        Text
        {
            text:"测试鼠标点击事件"
        }
        //鼠标响应区域
        MouseArea
        {
            anchors.fill: parent
            设置响应按键,默认是左键,多个按键用|连接
            //Qt.AllButtons
            //Qt.LeftButton
            //Qt.RightButton
            //Qt.MiddleButton
            acceptedButtons: Qt.AllButtons //Qt.LeftButton|Qt.MiddleButton
            /*
            clicked(MouseEvent mouse)
            doubleClicked(MouseEvent mouse)
            pressed(MouseEvent mouse)
            released(MouseEvent mouse)
            */
            onClicked:(mouse)=>
            {
              /*    Qt.LeftButton     0x00000001     0001
                    Qt.RightButton    0x00000002     0010
                    Qt.MiddleButton   0x00000004     0100
              */
                //左键按下事件
                if(mouse.button === Qt.LeftButton)
                {
                    print("LeftButton")
                    //按下左键时右键按住
                    if(mouse.buttons & Qt.RightButton)
                        print("RightButton")
                    //按下左键时右键和中键按住
                    if((mouse.buttons & Qt.RightButton )&&
                       (mouse.buttons & Qt.MiddleButton )
                       )
                    print("RightButton&MiddleButton")
                }
                //当鼠标事件进入时,键盘Ctrl Shift Alt是否按住
                //modifiers : int 同时按下的键盘功能键
                //    Qt.ShiftModifier - A Shift key on the keyboard is pressed.
                //    Qt.ControlModifier - A Ctrl key on the keyboard is pressed.
                //    Qt.AltModifier - An Alt key on the keyboard is pressed.
                //Ctrl+ Alt
                if( (mouse.modifiers & Qt.ControlModifier) &&
                    (mouse.modifiers & Qt.AltModifier)
                   )
                    print("Ctrl + Alt")
                print("onClicked" +
                      " button:" + mouse.button +
                      " buttons:"+ mouse.buttons
                      )
            }
            onDoubleClicked:(mouse)=>
            {
                print("onDoubleClicked")
            }
            onPressed:(mouse)=>
            {
                print("onPressed")
            }
            onReleased:(mouse)=>
            {
                print("onReleased")
            }
            /*onClicked:
            {
                print("MouseArea onClicked")
            }*/
        }
    }
    //测试鼠标移动事件
    Rectangle
    {
        x:120
        width:100
        height:100
        color:"#003300"
        Text {
           text: qsTr("测试鼠标移动事件")
        }
        MouseArea
        {
            hoverEnabled: true //鼠标未按键直接移入
            anchors.fill: parent
            acceptedButtons: Qt.AllButtons
            onEntered:
            {
                //containsMouse 鼠标在MouseArea区域内
                //containsPress 鼠标是否在区域内按下并按住
                print("onEntered "
                      +" containsMouse:" +containsMouse
                      +" containsPress:"+containsPress
                      )
                parent.color = "#00FF00"
            }
            onExited: parent.color = "#003300"
            //鼠标移动 移动就调用
            onPositionChanged:(mouse) =>
            {
                print(containsPress + " " +containsMouse+" "+ mouse.x+":"+mouse.y  )
                //判断按键按下
            }
            onPressAndHold: (mouse) => //按下鼠标按键进入一次
            {
                print("Hold "+mouse.x+":"+mouse.y)
            }
        }
    }
    //鼠标拖动事件
    Rectangle
    {
        id:container
        y:120
        width:  600
        height: 100
        color: "#999999"
        Rectangle
        {
            id:dragrect
            y:25
            width:50
            height:50
            color:"red"
            MouseArea
            {
                anchors.fill: parent
                drag.target: dragrect
                //限制在X轴拖动
                drag.axis: Drag.XAxis
                //限制拖动位置
                drag.minimumX: 0
                drag.maximumX:container.width - parent.width
            }
        }
    }
}


相关文章
|
3月前
QML 界面切换的方法
QML 界面切换的方法
186 1
|
3月前
qml Drag的使用
qml Drag的使用
32 0
|
5月前
Qt鼠标事件
Qt鼠标事件
|
5月前
Qml:键盘事件
Qml:键盘事件
|
6月前
|
C++
Qt6学习笔记十(鼠标事件、事件分发器)
Qt6学习笔记十(鼠标事件、事件分发器)
110 0
从零开始学Pyqt5之【控件介绍】(10):QDialog对话框控件
从零开始学Pyqt5之【控件介绍】(10):QDialog对话框控件
从零开始学Pyqt5之【控件介绍】(10):QDialog对话框控件