Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QML:Flickable的acceptedButtons_Qt_Event Handling_Qml_Mouseevent - Fatal编程技术网

Qt QML:Flickable的acceptedButtons

Qt QML:Flickable的acceptedButtons,qt,event-handling,qml,mouseevent,Qt,Event Handling,Qml,Mouseevent,我有一个Flickable,其中有些内容有自己的DragHandler 我希望所有使用鼠标左键的拖动都由内容的拖动处理程序处理,而使用鼠标右键的拖动则应由Flickable处理。可能吗 示例代码: Flickable { id: canvas anchors.fill: parent contentWidth: 2*parent.width contentHeight: 2*parent.height // Something like this

我有一个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
        }
    }
}
目标:我想用鼠标右键拖动,将蓝色的
矩形
移动到
Flickable
中,而用鼠标左键拖动事件则由蓝色
矩形

拖动者处理,我发现接受的按钮静态设置为左按钮:

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
        }
    }
}
它只是模仿标准行为,但允许使用
DragHandler
acceptedButtons
属性,用我想要的按钮控制
Flickable

到目前为止,我没有注意到
Flickable
的任何其他行为突破了这个解决方案