微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 ScrollView 和 Flow 中使用 MouseArea

如何解决如何在 ScrollView 和 Flow 中使用 MouseArea

我确实想使用 MouseArea 在 ScrollView 中的 Flow 中显示可点击的按钮。这种结构的原因是对象是从用户动态创建的,所以我想将它们放置在 Flow 中以确保垂直放置,并放置在 ScrollView 中以确保在变为多个对象时滚动。

    ScrollView {
            clip: true
            id: shiftsScrollView
    
            wheelEnabled: true
            //ScrollBar.vertical.policy: ScrollBar.AlwaysOn
    
            height: 100
            width: 300
            anchors.left: shiftsTitle.left
            anchors.top: shiftsTitle.bottom
            //spacing: 0
            anchors.leftMargin: 0
            anchors.topMargin: 20
    
            Flow {
                id: scrollableFlow
                layoutDirection: Qt.RightToLeft
                flow: Flow.TopToBottom

// Objects will be filled in here at runtime
    
            }
    
    
        }

这是在 js 函数中创建对象的方式:

for (let i=0; i<amountOfShifts; i++) {
            var myShift = Qt.createQmlObject(
                        'ShiftListElement {
                        width: 300;
                        height: 25;
                        shiftText: "'+splittedShifts[i]+'";
                        shiftName: "'+shiftNames[i]+'";

                        }',scrollableFlow,"shiftElement");
            shiftListObjects.push(myShift);
            shiftsScrollView.contentHeight = amountOfShifts * 25
        }

请注意 ID 'scrollableFlow' 是 ScrollView 中的 ID,您可以直观地看到它的工作原理。对象在之前彼此不认识的情况下按顺序垂直放置。

包含非工作鼠标区域的 QML 对象:

    Item {
        id: item1
    
        property string shiftText: "shift";
        property string shiftName: "name";
    
        width: 200
        height: 100
    
        Text {
            id: text1
            text: item1.shiftText
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: 12
            anchors.horizontalCenterOffset: -30
            anchors.horizontalCenter: parent.horizontalCenter
        }
    
        Button {
            id: button
            width: 20
            height: 20
            text: qsTr("X")
            anchors.verticalCenter: text1.verticalCenter
            anchors.left: text1.right
            anchors.leftMargin: 20
            MouseArea {
                id: buttonCommitShiftMouseArea
                anchors.fill: parent
                preventStealing: true
                onClicked:
                {
    
                   console.log("I work!");
    
                }
            }
        }
    
    }

我找到了一些推荐属性“preventStealing”的答案,但在这种情况下它似乎没有效果。我也尝试删除 Flow,这似乎也没有什么区别。它确实在常规项目中的 ScrollView 之外工作,这让我认为 ScrollView 窃取了其中的所有 MouseEvents。 ScrollView 可根据需要滚动并以所需大小显示

我错过了什么吗?感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?