如何解决QML:接受的 Flickable 按钮
我有一个 Flickable,其中一些内容有自己的 DragHandler
。
我希望鼠标左键的所有拖动都由内容的拖动处理程序处理,而鼠标右键的拖动应该由 Flickable 处理。这可能吗?
示例代码:
Flickable {
id: canvas
anchors.fill: parent
contentWidth: 2*parent.width
contentHeight: 2*parent.height
// Something like this
//acceptedButtons: Qt.RightButton
Rectangle {
color: "blue"
width: canvas.width
height: canvas.height
DragHandler {
acceptedButtons: Qt.LeftButton
target: null
onActiveChanged: console.log(`content drag active: ${active}`)
// Some code to do something with the drag...
}
Rectangle {
color: "yellow"
anchors.centerIn: parent
width: 50
height: 50
}
}
}
目标:我想通过鼠标右键拖动移动Rectangle
内的蓝色Flickable
,而鼠标左键拖动事件由DragHandler
处理蓝色Rectangle
解决方法
查看 QQuickFlickable source code,我发现接受的按钮被静态设置为左侧按钮:
272: q->setAcceptedMouseButtons(Qt::LeftButton);
所以我尝试通过引入另一个对我有用的 DragHandler
来解决这个问题:
Flickable {
id: canvas
anchors.fill: parent
contentWidth: 2*parent.width
contentHeight: 2*parent.height
DragHandler {
property real _startX
property real _startY
acceptedButtons: Qt.RightButton
dragThreshold: 0
target: null
onActiveChanged: {
if (active) {
_startX = canvas.contentX
_startY = canvas.contentY
}
}
onTranslationChanged: {
canvas.contentX = _startX - translation.x
canvas.contentY = _startY - translation.y
}
}
Rectangle {
color: "blue"
width: canvas.width
height: canvas.height
DragHandler {
acceptedButtons: Qt.LeftButton
target: null
onActiveChanged: console.log(`content drag active: ${active}`)
// Some code to do something with the drag...
}
Rectangle {
color: "yellow"
anchors.centerIn: parent
width: 50
height: 50
}
}
}
它只是模仿标准行为,但允许使用 acceptedButtons
的 DragHandler
属性通过我想要的按钮控制 Flickable
。
到目前为止,我没有注意到 Flickable
突破此变通方法的任何其他行为。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。