首页 文章

Flickable中的MouseArea阻止它轻弹

提问于
浏览
6

我正在使用MouseArea实现手势捕捉器(向左/向右滑动) . 它应该在Flickable内部使用垂直flickableDirection . 此外,它应该以可视堆栈顺序将鼠标事件传播到其下的其他元素 . 问题是将 propagateComposedEvents 设置为true的child mouseArea阻止任何父级的弹出,然后才能进行 exact one 点击 . 首次点击后,它正常工作 . 这是显示此内容的简化代码 .

import QtQuick 2.4
import QtQuick.Window 2.2

Window {
    id: __root
    visible: true
    width: 460; height: 640

    Flickable {
        id: mainFlickable

        width: parent.width
        height: parent.height
        contentHeight: column.height
        flickableDirection: Flickable.VerticalFlick

        MouseArea {
            anchors.fill: parent
            propagateComposedEvents: true
            z: 1
        }

        Column {
            id: column

            width: parent.width

            Repeater {
                model: 5

                Rectangle {
                    width: __root.width
                    height: 200

                    color: "yellow"
                    border.width: 2

                    MouseArea {
                        anchors.fill: parent

                        onClicked: {
                            console.log("clicked")
                        }
                    }
                }
            } //repeater
        } //column
    } //flickable
} //window

我花了很多时间试图解决这个问题,并会感谢任何帮助 . 提前致谢!

2 回答

  • 5

    我发现在MouseArea中跟随信号处理程序是一种解决方法,不要破坏我的代码:

    onReleased: {
        if (!propagateComposedEvents) {
            propagateComposedEvents = true
        }
    }
    

    propagateComposedEvents 应在声明(或ommited)上设置为 false .

    谢谢大家的努力!

  • 1

    我找到了一点解决方法 . 希望它能满足您的需求(至少在提供更好的解决方案之前) .

    这是您更新的代码:

    import QtQuick 2.4
    import QtQuick.Window 2.2
    
    Window {
        id: __root
        visible: true
        width: 460; height: 640
    
        Flickable {
            id: mainFlickable
    
            width: parent.width
            height: parent.height
            contentHeight: column.height
            flickableDirection: Flickable.VerticalFlick
    
            onDragStarted: ma.enabled = false
            onDragEnded: ma.enabled = true
    
            MouseArea {
                id: ma
                anchors.fill: parent
                enabled: false
                propagateComposedEvents: true
                z: 100
    
                onClicked: {
                    print("CLICKED ON UPPER")
                    mouse.accepted = false
                }
            }
    
            Column {
                id: column
    
                width: parent.width
    
                Repeater {
                    model: 5
    
                    Rectangle {
                        width: __root.width
                        height: 200
    
                        color: "yellow"
                        border.width: 2
    
                        MouseArea {
                            anchors.fill: parent                  
                            onClicked: console.log("clicked on child") 
                        }
                    }
                } //repeater
            } //column
        } //flickable
    } //window
    

相关问题