我有几个QML项目都有鼠标区域。
我想要实现的目标是:
示例代码:
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
width:500; height: 200;
Rectangle{
anchors.left: parent.left
color: 'red'
width: 200; height: 200;
MouseArea {
anchors.fill: parent
hoverEnabled: true
onReleased: console.log('onReleased red')
onEntered: console.log('onEntered red')
onPressed: {
console.log('onPressed red')
mouse.accepted = false
}
}
}
Rectangle{
anchors.right: parent.right
color: 'blue'
width: 200; height: 200;
MouseArea {
anchors.fill: parent
hoverEnabled: true
onReleased: console.log('onReleased blue')
onEntered: console.log('onEntered blue')
onPressed: console.log('onPressed blue')
}
}
}
预期行为:
示例代码都有我试过的版本,有或没有接受mousePressed
事件。
会发生什么:
如果我将鼠标按在一个矩形上,我的所有其他矩形都不会得到onEnter
事件。
如果我不接受onPressed
活动,则会收到onEnter
个活动,但不会收到onReleased
活动。
注意:
我已经找到this Answer使用DropArea作为解决方法,如果有任何其他解决方案,这不是我想要使用的。
即使示例看起来像Drag& Drop,它也不是我想要的。 请参阅本课题顶部的“我想达到的目标”。
答案 0 :(得分:2)
您将无法使用标准 MouseArea 组件实现您想要的效果。 Standart QML组件的功能有限。 您需要做的是通过QML扩展创建自己的 MouseArea 组件。 在我们的项目中,我们也遇到了很多鼠标处理问题,所以我们设法做到了:
Subclassed QQuickItem,在这个类里面我们只跟踪鼠标移动和鼠标按钮状态。一个重要的事情是安装这个类定义的EventFilters。
在QML中,创建了一个简单组件,用于检查鼠标是否在currrent组件内。
如果您需要,我也可以在这里发布代码,以便您有一个想法。 实现并不是最漂亮的,但它可以正常工作