QML之MouseArea事件

本文介绍如何使用QML中的MouseArea组件实现鼠标事件处理,包括点击、双击、长按等常见事件,并展示了如何设置鼠标作用域、按键及拖动等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QML的鼠标事件是通过不可见元素MouseArea来实现,常用的事件有点击,双击,长按等。常用的设置有鼠标作用域设置,鼠标按键设置等。下面代码详细说明

Rectangle{
        id:rec_test
        height: 300
        width: 300
        radius: 5
        color: "gray"
        anchors.centerIn: parent
        Text {//配合鼠标事件操作
            id: test
            anchors.centerIn: parent
            font.pixelSize: 35
            color: mous.pressedButtons ===Qt.RightButton ? "red" : "blue"
            //pressedButtons为只读属性,值为枚举值,代表按下时哪一个按键
            text: "Mouse"
        }
        Text {//
            id: test_mous
            font.pixelSize: 20
            color: "blue"
            text: "mouseX:" +mous.mouseX + "\nmouseY:" + mous.mouseY
            //显示鼠标点击区域的坐标,为只读属性
        }
        Text {//
            id: test_mouspre
            anchors.top: test_mous.bottom
            font.pixelSize: 20
            color: "blue"
            text: "pressed:" +mous.pressed
            //显示鼠标是否被按下,为只读属性,按下时为true,反之false
        }
        Text {//
            id: test_mouscon
            anchors.top: test_mouspre.bottom
            font.pixelSize: 20
            color: "blue"
            text: "ContainsMouse:" +mous.containsMouse
            //显示鼠标是否在作用区域内,为只读属性,注意不开启悬浮功能的话,只有按下才改变
        }
        Text {//
            id: test_dra
            anchors.top: test_mouscon.bottom
            font.pixelSize: 20
            color: "blue"
            text: "drag.active:" +mous.drag.active
            //显示指定拖动目标的活动状态,是否在被拖动
        }



        MouseArea{
            id:mous
            anchors.fill: parent //鼠标的作用区域,也可以用宽与高属性来设置
            enabled: true //是否接受鼠标事件,默认true
            //hoverEnabled: true //是否处理悬浮事件,也就是没有点击时候的时间,例如onPositionChanged,默认false
            preventStealing: true //是否防止鼠标时间被窃取,默认为false,false的时候可以被窃取。
            //acceptedButtons: Qt.RightButton | Qt.LeftButton //设置鼠标的触发区域,例如左键右键等,默认左键
            //acceptedButtons: Qt.MiddleButton //中间滑轮的那个按键
            //acceptedButtons: Qt.XButton1 | Qt.XButton2//这两个按键应该是鼠标边边的按键,没测试,因为我的鼠标上没有
            drag.target: rec_dra //提供一个拖动目标,拖动目标不能使用锚定位,不会拖动无效
            drag.axis: Drag.XandYAxis//Drag.YAxis//Drag.XAxis //选择拖动方式,顾名思义
            drag.minimumX: 0   //拖动区域的最小X坐标(相对父对象的坐标)
            drag.maximumX: parent.width - rec_dra.width //拖动区域的最大X坐标
            drag.minimumY: 0  //拖动区域的最小Y坐标
            drag.maximumY: parent.height - rec_dra.height //拖动区域的最大Y坐标
            drag.filterChildren: true

            //下面是点击等信号处理槽
            onCanceled: {console.log("onCanceled");}//鼠标时间取消或者被窃取时触发。
            onClicked: {console.log("onClicked");}  // 点击时触发
            onDoubleClicked: {console.log("onDoubleClicked");} //双击触发
            onEntered: {console.log("onEntered");} //进入鼠标区域触发,悬浮属性为false,需点击才触发
            onExited: {console.log("onExited");}   //退出鼠标区域(hoverEnabled得为true),或者点击退出的时触发
            onPressAndHold: {console.log("onPressAndHold");} //长按触发,默认时间800ms
            onPressed: {console.log("onPressed");}          //按下就能触发
            onReleased: {console.log("onReleased");}        //释放时触发
            onPositionChanged: {console.log("onPositionChanged");} //鼠标位置变化(hoverEnabled得为true),不然得点击才变化

            Rectangle{ //提供被拖拽
                id:rec_dra
                height: 50
                width: 50
                y:200
                x:100
//                anchors.horizontalCenter: parent.horizontalCenter
//                anchors.bottom: parent.bottom
                color: "red"
            }

        }

    }


### QMLMouseArea事件穿透方法 在 QML 中,`MouseArea` 组件可以配置为允许鼠标事件传递到其下方的其他 `MouseArea` 或者组件。这通过设置 `propagateComposedEvents` 属性为 `true` 来实现[^1]。 当设置了 `propagateComposedEvents: true` 后,在处理特定鼠标事件(如点击、按下或释放)时,可以通过将 `mouse.accepted = false` 设置来让当前 `MouseArea` 不接受该事件,从而使事件继续传播给下层的对象。 然而,如果上方的 `MouseArea` 占用了整个区域并阻止了底层控件接收任何输入,则即使启用了上述机制也可能出现问题[^2]。为了确保底层页面能够正常接收到鼠标交互信号而不被阻挡,需谨慎设计各层之间的布局关系以及各自响应逻辑。 #### 示例代码展示如何正确配置 MouseArea 进行事件穿透: ```qml Item { width: 300; height: 300 Rectangle { // 底层矩形 id: bottomRect color: "lightblue" anchors.fill: parent MouseArea { anchors.fill: parent onClicked: console.log("Bottom rectangle clicked!") propagateComposedEvents: true // 允许组合事件传播 } } Rectangle { // 上层透明矩形 id: topTransparentRect opacity: 0.5 color: "gray" anchors.centerIn: parent width: 200; height: 200 MouseArea { anchors.fill: parent onClicked: { mouse.accepted = false // 让点击事件向下一层传递 // 可在此处添加额外操作... console.log("Top transparent rect was clicked but event passed through.") } } } } ``` 此示例展示了两个重叠放置的矩形,其中上层矩形部分透明以便观察效果。当用户单击上层矩形时,由于设置了 `mouse.accepted = false` 和 `propagateComposedEvents: true`,因此点击事件会被转发到底部矩形对应的 `MouseArea` 处理程序中去执行相应动作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值