首页 文章

如何在QML中处理来自父Flickable的gridview内容Y,Y位置

提问于
浏览
0

我有一个backGroundList和一个Flickable顶部gridview .

我想要处理来自父Flickable的gridview contentY,Y位置,当我将gridview高度指定为内容高度时它正在工作但是由于内存问题我以前不想创建项目 .

码:

Flickable {
            id: mainFlickable
            width: 1920
            height: 1080
            contentHeight: gridView.contentHeight + 660 + 140 // topmargin + bottom margin

            ListView {
                id:backGroundList
                width: 1920
                height: 1080
                y: mainFlickable.contentY
                orientation: ListView.Horizontal

                MouseArea {
                    id: myMA12
                    anchors.fill: parent
                    hoverEnabled: true
                }
            }

            GridView {
                id: gridView
                objectName: "gridView"

                width: 1920
                height: 1080



                cellWidth: 372
                cellHeight: 290

                interactive: false
                 y: {
                    if (mainFlickable.contentY > 660)
                        return 660 + (mainFlickable.contentY - 660)
                    else
                        return 660
                }

                contentY: {
                    if (mainFlickable.contentY > 660){
                        return (mainFlickable.contentY - 660)
                    }
                    else {
                        return 0
                    }
                }

                delegate: Rectangle {
                    width: 352
                    height: 270
                    color: "blue"
                    opacity: 0.5

                    Text {
                        text: index
                        anchors.centerIn: parent
                    }
                }
                model:100
                anchors {
                    left: parent.left
                    leftMargin: 30
                    right: parent.right
                    rightMargin: 30
                }
            }
}

它没有按预期工作 . When I update Y value ContentY is updating to zero automatically

约束:Qt5.9.0

有人可以帮我这个 .

1 回答

  • 0

    如果我理解你的话,你想要两个 ...View 一个在另一个上面 . 当其中一个人轻弹到最后时,你想让那个 View 从窗口中弹出,第二个弹出,然后开始轻弹 .

    在我看来,如果将其他两个嵌套在一起,这并不是一件容易的事 .

    使用 Flickable 很容易实现它,这是他们的兄弟姐妹 . 在我的示例代码中,我使用了两个Flickable . 一个用于控制 contentY -properties,另一个用于在两个 View 之间滚动 . 只需使用 Item 即可轻松,高效地解决后期问题 .

    Window {
        id: rootWin
        width: 800; height: 600; visible: true
    
        Flickable {
            id: flck
            anchors.fill: parent
            contentHeight: lv1.height + lv2.height
            interactive: false
            contentY: Math.min(Math.max(0, mainFlick.contentY - lv1.contentHeight + lv1.height), contentHeight - height)
    
            ListView {
                id: lv1
                height: rootWin.height
                width: 100
                model: 90
                delegate: Rectangle {
                    border.width: 1
                    width: 100
                    height: 50
                    Text {
                        anchors.centerIn: parent
                        text: index
                    }
                }
                interactive: false
                contentY: Math.min(mainFlick.contentY, contentHeight - height)
                onContentYChanged: console.log(contentY, contentHeight, height)
            }
            ListView {
                id: lv2
                height: rootWin.height
                anchors.top: lv1.bottom
                width: 100
                model: 90
                delegate: Rectangle {
                    border.width: 1
                    width: 100
                    height: 50
                    Text {
                        anchors.centerIn: parent
                        text: index
                    }
                }
                interactive: false
                contentY: Math.min(Math.max(0, mainFlick.contentY - lv1.contentHeight), contentHeight - height)
            }
        }
        Flickable {
            id: mainFlick
            anchors.fill: parent
            contentHeight: lv1.contentHeight + lv2.contentHeight
        }
    }
    

相关问题