我有一个有自己的DragHandler
内容的Flickable.
我希望所有拖动鼠标左键的内容都由内容的拖动处理程序处理,拖动鼠标右键的内容应该由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
内部,而鼠标左键拖动事件由蓝色Rectangle
的DragHandler
处理
查看QQuickFlickable源代码,我发现接受的按钮被静态地设置为左侧按钮:
272:问→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
}
}
}
它只是模仿标准行为,但允许使用DragHandler
的acceptedButtons
属性来用我想要的按钮控制Flickable
。
到目前为止,我没有注意到Flickable
的任何其他行为突破了这个解决方案。