向 ScrollView 中的 ColumnLayout 添加动态对象

问题描述 投票:0回答:1

我有一个 ScrollView,它处理一个包含 4 个 ColumnLayout 的 RawLayout。 有几个通过

Qt.createComponent(...)
动态创建的矩形,它们被添加到列中。

我被困住了,试图动态地将对象添加到Column中,所以ScrollView应该处理Column不断增长的内容,但它似乎不起作用,因为当

Qt.createComponent(...).createObject(parentID)
parentID
- 是我的Column的id,而不是ScrollView的(根据文档,这是必需的)。

import QtQuick 2.15
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.15

Rectangle {
    id: root
    //anchors.fill: parent
    height: 1600
    width: 1400
    color: "black"

    function createTestModule()
    {
        var component = Qt.createComponent("TestModule.qml"); //TestModule.qml is just a red rectangle
        if (component.status === Component.Ready)
            var obj = component.createObject(testLyt);
        else
            console.log(component.errorString())
    }

    Rectangle {
        id: buttonContainer
        width: 60
        height: 15
        color: "darkgrey"
        z: 1

        Button {
            width: parent.width
            height: parent.height
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter

            Text {
                text: qsTr("TestModule+")
                color: "black"
                font.pointSize: 8
                width: parent.width
            }

            onClicked: {
                createTestModule();
            }
        }
    }

    ScrollView
    {
        id: scroll
        width: 400
        height: 400
        ScrollBar.horizontal.policy: ScrollBar.AlwaysOn
        ScrollBar.vertical.policy: ScrollBar.AlwaysOn

        Rectangle {
            id: container
            color: "dimgray"
            //anchors.top: buttonContainer.bottom
            width: parent.width
            height: parent.height
            smooth: true

            Row {
                id: rowLyt
                spacing: 0

                Column
                {
                    id: testLyt
                    spacing: 10
                }
                Column
                {
                    id: testLyt1
                    spacing: 10
                }
                Column
                {
                    id: testLyt2
                    spacing: 10
                }
                Column
                {
                    id: cubicLyt3
                    spacing: 10
                }
            }
        }
    }
}

qt qml
1个回答
0
投票

问题原来是在ScrollView中需要直接包裹Layout,而不是容器(矩形):

Rectangle {
  ScrollView {
    Row {
     Column {} 
     Column {} 
     Column {} 
    }  
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.