Populate GridLayout with Repeater

前端 未结 3 1056
广开言路
广开言路 2021-01-01 04:07

I try to add cells to my GridLayout by using a Repeater. My data is stored in a model and containing two properties per element:

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 04:25

    The model-view principle assumes that each model node displays by different delegate component object. So I advice you to listen to @BaCaRoZzo's comment and do that with Column instead of GridLayout. Sure, QML is very flexible and you can do something like that:

    Component {
        id: labelDelegate
        Label { text: myList.get(_index / 2).title }
    }
    
    Component {
        id: textAreaDelegate
        TextArea { text: myList.get(_index / 2).value }
    }
    
    ListModel {
        id: myList
        ListElement {title: "title1"; value: "value1"}
        ListElement {title: "title2"; value: "value2"}
        ListElement {title: "title3"; value: "value3"}
    }
    
    GridLayout {
        anchors.fill: parent
        columns: 2
        Repeater {
            model: myList.count * 2
            delegate: Loader {
                property int _index: index
                sourceComponent: {
                    if(index % 2)
                        return textAreaDelegate;
                    else
                        return labelDelegate;
                }
            }
        }
    }
    

    but that's too weird to use it in real project.

提交回复
热议问题