GridLayout布置

问题描述

以下是我的 main.qml

Window {
    id: window
    visible: true
    width: 800
    height: 480
    title: qsTr("Hello World")
    ListModel {
        id: _listModel
        ListElement {
            textData: "E1"
            isEnabled: false
        }
        ListElement {
            textData: "E2"
            isEnabled: false
        }
        ListElement {
            textData: "E3"
            isEnabled: false
        }
        ListElement {
            textData: "E4"
            isEnabled: false
        }
        ListElement {
            textData: "E5"
            isEnabled: false
        }
        ListElement {
            textData: "E6"
            isEnabled: false
        }
    }

    ListView {
        id: _listview
        model: _listModel
        width: 100
        height: parent.height
        anchors.right: parent.right
        delegate: Item {
            width: parent.width
            height: 50
            anchors.right: parent.right
            Component.onCompleted:
            {
                if (isEnabled)
                    visibleRecs++;
            }

            RowLayout {
                Text {
                    id: itemText
                    text: qsTr(textData)
                }
                CheckBox {
                    height: 30
                    width: height
                    checked: isEnabled
                    onCheckedChanged: {
                        isEnabled = checked
                    }
                }
            }
        }
    }

    ScrollView {
        id: _scrollView
        width: parent.width / 2
        height: parent.height
        clip: true
        GridLayout {
            id: _gridLayout
            anchors.fill: parent
            anchors.horizontalCenter: parent.horizontalCenter
            columnSpacing: 10
            rowSpacing: 10
            columns: 2

            Repeater {
                model: _listModel
                Loader {
                    id: _loader
                    sourceComponent: isEnabled ? _recComponent : null
                    onLoaded: {
                        item.text = textData
                    }
                }
            }
        }
    }



    Component {
        id: _recComponent
        Rectangle {
            property alias text : _txt.text
            id: _rec
            width: 100
            height: 50
            radius: 5
            color: "yellow"
            Text {
                id: _txt
                anchors.centerIn: parent
            }
        }
    }
}

上面的代码创建以下内容(选中所有复选框时):
勾选所有复选框:

enter image description here



未选中复选框E3:

enter image description here


如果任何项目变得不可见,我希望这些项目在网格布局中重新排列。
例如。在上述情况下,当未选中E3时,我希望我的视图是这样的:

enter image description here

如果可以实现,请告诉我。预先感谢。

解决方法

问题在于您仍然实例化Loader,只是将sourceComponent设置为null。您必须使该项不可见,以免使用GridLayout中的空格(或将width / height设置为0)

ScrollView {
    id: _scrollView
    width: parent.width / 2
    height: parent.height
    clip: true
    GridLayout {
        id: _gridLayout
        anchors.fill: parent
        anchors.horizontalCenter: parent.horizontalCenter
        columnSpacing: 10
        rowSpacing: 10
        columns: 2

        Repeater {
            model: _listModel
            Loader {
                id: _loader
                visible: isEnabled
                sourceComponent: isEnabled ? _recComponent : null
                onLoaded: {
                    item.text = textData
                }
            }
        }
    }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...