问题描述:
在工作中遇到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;
}