QML:Flickable 的 acceptedButtons

QML: acceptedButtons for 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 内部的蓝色 Rectangle,而鼠标左键的拖动事件由 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
        }
    }
}

它只是模仿标准行为,但允许使用 DragHandleracceptedButtons 属性 用我想要的按钮控制 Flickable
到目前为止,我没有注意到 Flickable 突破此解决方法的任何其他行为。