QML MouseArea中双击事件中丢失onReleased事件解决方案

问题描述:

在工作中遇到ListView中Item项双击后,弹窗操作,再次操作Item项时,需要单独点击一次鼠标后才能正常操作。

排查过程:

模拟代码如下

import QtQuick 2.15
import QtQuick.Controls 2.4

Item {
    property bool isDoubleClicked: false
    signal objDoubleClicked()
    signal objItemSelected(var idx)         //单选

    Timer{
        id: clickTimer
        interval: 300
        onTriggered: {
            clickTimer.stop(); 

            objItemSelected(index);
        }
    }

    MouseArea{
        id:idMainArea
        anchors.fill: parent

        propagateComposedEvents: true
        cursorShape: Qt.ArrowCursor

        acceptedButtons: Qt.LeftButton | Qt.RightButton

        onPressed:{
            console.log("[shorlly]HiListViewItem onPressed!");
        }
        onReleased:{
            console.log("[shorlly]HiListViewItem onReleased!");
        }
        onClicked: {
            console.log("[shorlly]HiListViewItem onClicked!");
            clickTimer.start();
        }
        onDoubleClicked: {
            console.log("[shorlly]HiListViewItem onDoubleClicked!");
            clickTimer.stop();
            objDoubleClicked();
        }
    }

}

正常双击过程输出为(收到双击事件后无后续弹窗)

[shorlly]HiListViewItem onPressed!

[shorlly]HiListViewItem onReleased!

[shorlly]HiListViewItem onClicked!

[shorlly]HiListViewItem onPressed!

[shorlly]HiListViewItem onDoubleClicked!

[shorlly]HiListViewItem onReleased!

异常双击过程输出为(收到双击事件后弹出另一个qml窗口,焦点聚焦到新qml)

[shorlly]HiListViewItem onPressed!

[shorlly]HiListViewItem onReleased!

[shorlly]HiListViewItem onClicked!

[shorlly]HiListViewItem onPressed!

[shorlly]HiListViewItem onDoubleClicked!

推断因焦点转移,造成第二次onReleased事件丢失。

再次点击该item项时,输出

[shorlly]HiListViewItem onReleased!(onPressed事件无效)

解决方案

采用逻辑控制,在第二次onReleased事件后再通知doubleclicked事件,切换焦点

摘抄修改部分代码如下

        onReleased:{
            console.log("[shorlly]HiListViewItem onReleased!");
            if (isDoubleClicked){
                objDoubleClicked();
                isDoubleClicked = false;
            }
        }

        onDoubleClicked: {
            console.log("[shorlly]HiListViewItem onDoubleClicked!");
            clickTimer.stop();
            isDoubleClicked = true;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值