qtqmlqtquick2

QML ListView section works in 6.5.1 but not in 5.15.2


The following code works as expected in 6.5.1 but not in 5.15.2

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    ListView {
        anchors.fill: parent
        model: fakeModel
        delegate: Text {
            text: modelData.name
        }
        section.property: "blockName"
        section.criteria: ViewSection.FullString
        section.delegate: Text {
            text: section
            color: "red"
        }
    }

    property var fakeModel: [{
            "blockName": "ABC",
            "name": "Lorem Ipsum"
        }, {
            "blockName": "ABC",
            "name": "Front Raises"
        }, {
            "blockName": "ZXC",
            "name": "Band Tall "
        }, {
            "blockName": "ZXC",
            "name": "Overhead"
        }]
}

I'm using EndeavourOS Linux 6.1.55-1-lts

in 5.15.2:

in 5.15.2

in 6.5.2:

in 6.5.2

Does anyone know if there is any work around for making the listview sections work with qt5?

Thanks


Solution

  • As you indicated in the comments, for Qt5.15.x use ListModel. If you wish to keep your declarative array, you can read and convert it to populate your ListModel, e.g.

        ListView {
            anchors.fill: parent
            model: ListModel {
                function load(arr) { clear(); arr.forEach(e => append(e)) }
                Component.onCompleted: load(array_data)
            }
            delegate: Text {
                text: name
            }
            section.property: "blockName"
            section.criteria: ViewSection.FullString
            section.delegate: Text {
                text: section
                color: "red"
            }
        }
        
        property var array_data: [
            { "blockName": "ABC", "name": "Lorem Ipsum" },
            { "blockName": "ABC", "name": "Front Raises" },
            { "blockName": "ZXC", "name": "Band Tall " },
            { "blockName": "ZXC", "name": "Overhead" }
        ]