QtQuick2:在ScrollView内部处理onWheel事件

QtQuick2:在ScrollView内部处理onWheel事件,qt,qml,qtquick2,Qt,Qml,Qtquick2,我必须把X组件放在滚动视图中。组件X必须处理鼠标滚轮事件,但ScrollView会处理它。所以,下面的简化示例不起作用 如何让矩形的鼠标区域处理OnWheel事件 import QtQuick 2.1 import QtQuick.Controls 1.0 import QtQuick.Window 2.0 import QtQuick.Layouts 1.0 ApplicationWindow { width: 640 height: 480 ScrollView

我必须把X组件放在滚动视图中。组件X必须处理鼠标滚轮事件,但ScrollView会处理它。所以,下面的简化示例不起作用

如何让矩形的鼠标区域处理OnWheel事件

import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    width: 640
    height: 480

    ScrollView {
        height: 100
        width: 100

        ColumnLayout{
            Rectangle {
                color: "red"
                width: 50
                height: 50
                MouseArea {
                    anchors.fill: parent
                    onWheel: {
                        console.log("onWheel"); // it doesn't work
                    }
                    onClicked: {
                        console.log("onClicked"); // it works
                    }
                }
            }
        }
    }
}

我找到了解决问题的办法,但我不能很好地解释它

说明了可视父对象和对象父对象的概念,但没有说明它们如何影响事件传播

希望有人能解释清楚

ApplicationWindow {
    width: 640
    height: 480

    ScrollView {
        id: scroll   // add an id
        height: 100
        width: 100

        ColumnLayout{
            Rectangle {
                id: rect   // add an id
                color: "red"
                width: 50
                height: 50
                MouseArea {
                    parent: scroll      // specify the `visual parent`
                    anchors.fill: rect       // fill `object parent` 
                    onWheel: {
                        console.log("onWheel"); // now it works
                    }
                    onClicked: {
                        console.log("onClicked"); // it works
                    }
                }
            }
            Repeater {
                model: 30
                Text{ text: index }
            }
        }
    }  
}

我找到了解决问题的办法,但我不能很好地解释它

说明了可视父对象和对象父对象的概念,但没有说明它们如何影响事件传播

希望有人能解释清楚

ApplicationWindow {
    width: 640
    height: 480

    ScrollView {
        id: scroll   // add an id
        height: 100
        width: 100

        ColumnLayout{
            Rectangle {
                id: rect   // add an id
                color: "red"
                width: 50
                height: 50
                MouseArea {
                    parent: scroll      // specify the `visual parent`
                    anchors.fill: rect       // fill `object parent` 
                    onWheel: {
                        console.log("onWheel"); // now it works
                    }
                    onClicked: {
                        console.log("onClicked"); // it works
                    }
                }
            }
            Repeater {
                model: 30
                Text{ text: index }
            }
        }
    }  
}

这实际上是Qt中的一个bug:

这一问题正在以下方面得到解决:


这实际上是Qt中的一个bug:

这一问题正在以下方面得到解决:


感谢响应-我会尽快尝试。onWheel处理程序中应该有wheel.accepted=false,因为处理onWheel会破坏ScrollView行为。感谢响应-我会尽快尝试。onWheel处理程序中应该有wheel.accepted=false,因为处理onWheel会破坏ScrollView行为。