qml模态弹窗过滤鼠标移动事件

问题描述:在使用qml中的popup组件时,将modal属性设置成true,即模态弹窗,popup窗口之外区域的鼠标点击事件被禁用,但是鼠标移动事件仍然被响应,即被弹窗掩盖的id为ma1的MouseArea的onEntered和onExited事件仍然会触发。

代码示例:

Item{
    width: 100
    height: 100
    MouseArea{
        id: ma1
        hoverEnabled: true
        anchors.fill: parent
        onEntered:{
            console.log("鼠标移入")
        }
        onExited:{
            console.log("鼠标移出")
        }
        onClicked:{
            if(testPopup.opened){
                testPopup.open()
            }else{
                console.log("自动关闭弹窗")
                testPopup.close()
            }
        }
    }
    
    Popup {
        id: testPopup
        modal: true
        contentWidth: 30
        contentHeight: 30
        contentItem: Rectangle{
            anchors.fill: parent.contentItem
            MouseArea{
                id: ma2
                anchors.fill: parent
                onClicked:{
                    console.log("手动关闭弹窗")
                    testPopup..close()
                }
            }
        }
    }
}

解决方案:通过使用Overlay附加类型控制模态弹窗的遮蔽区域(也能设置非模态弹窗的遮蔽区域或者弹窗位置等属性,具体可以去官方文档中查看),本文中用的是模态弹窗,因此使用的是附加类型Overlay的modal属性,具体代码如下:

Item{
    width: 100
    height: 100
    MouseArea{
        id: ma1
        hoverEnabled: true
        anchors.fill: parent
        onEntered:{
            console.log("鼠标移入")
        }
        onExited:{
            console.log("鼠标移出")
        }
        onClicked:{
            if(testPopup.opened){
                testPopup.open()
            }else{
                console.log("自动关闭弹窗")
                testPopup.close()
            }
        }
    }
    
    Popup {
        id: testPopup
        modal: true
        contentWidth: 30
        contentHeight: 30
        contentItem: Rectangle{
            anchors.fill: parent.contentItem
            MouseArea{
                id: ma2
                anchors.fill: parent
                onClicked:{
                    console.log("手动关闭弹窗")
                    testPopup..close()
                }
            }
        }
        Overlay.modal: Rectangle{//设置模态弹窗遮蔽颜色
             color: "transparent"
             MouseArea{
                 preventStealing: true
             }
        }
    }
}

上述代码中,modal后的矩形组件用于设置遮蔽颜色及鼠标区域,鼠标区域中的preventStealing属性的作用在文档中直译为 “此属性用于控制该鼠标区域的鼠标事件是否被窃取”,这里设置为true实现了我想要的效果:防止任何鼠标事件被其他鼠标区域获取。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QML中,Popup是一种弹出式界面控件的基本类型,可以与Window或ApplicationWindow一起使用。一个基本的Popup示例可以如下所示: ```qml import QtQuick.Window import QtQuick.Controls ApplicationWindow { id: window width: 400 height: 400 visible: true Button { text: "Open" onClicked: popup.open() } Popup { id: popup x: 100 y: 100 width: 200 height: 300 modal: true focus: true closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent } } ``` 默认情况下,按下Escape或Back键会关闭Popup。如果您希望自定义处理这些事件,有两种解决方案: 1. 将Popup的closePolicy属性设置为不包括Popup.CloseOnEscape的值,这样按下退出键时不会关闭Popup。 2. 处理Keys的shortcutOverride信号,并在Popup之前接受事件。 Popup的closePolicy属性是一个枚举类型,决定了弹出窗口关闭的情况。以下是closePolicy的可能取值: - Popup.NoAutoClose: Popup只会在手动指示时关闭。 - Popup.CloseOnPressOutside: 当鼠标在Popup外部按下时,Popup将关闭。 - Popup.CloseOnPressOutsideParent: 当鼠标在父级之外按下时,Popup将关闭。 - Popup.CloseOnReleaseOutside: 当鼠标离开Popup时,Popup将关闭。 - Popup.CloseOnReleaseOutsideParent: 当鼠标在父级之外释放时,Popup将关闭。 - Popup.CloseOnEscape: 当Popup具有焦点且按下退出键时,Popup将关闭。123 #### 引用[.reference_title] - *1* *2* [QML控件类型:Popup](https://blog.csdn.net/kenfan1647/article/details/122816454)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [QML Popup详解](https://blog.csdn.net/wzz953200463/article/details/129409996)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值