0

I'm trying to create a list and append values to it that I've retrieved from xml http request. I've tested just a text block with the value of typeAssetProcess and it prints fine, but when I start trying to use a list is when everything starts to break. What am I doing wrong and how can I fix it?

import QtQuick 2.0
import "../controls" as Controls
Item {

Column {
    id: column

    width: parent.width
    height: parent.height
}

ListView {
    id: listView

    width: parent.width
    height: parent.height
    model: ListModel {
        ListElement {
            name: qstr("Proccess: %1").arg(typeAssetProcess)
            colorCode: "grey"
        }

        ListElement {
            name: "Red"
            colorCode: "red"
        }

        ListElement {
            name: "Blue"
            colorCode: "blue"
        }

        ListElement {
            name: "Green"
            colorCode: "green"
        }
    }
    delegate: Item {
        x: 5
        width: 80
        height: 40
        Row {
            id: row1
            Rectangle {
                width: 40
                height: 40
                color: colorCode
            }

            Text {
                text: name
                font.bold: true
                anchors.verticalCenter: parent.verticalCenter
            }
            spacing: 10
        }
    }
}

}

This code is what is breaking:

ListElement {
    name: qstr("Proccess: %1").arg(typeAssetProcess)
    colorCode: "grey"
}
1

1 Answer 1

1

This is a known limitation to the ListElement type, containing a "collection of role definitions instead of properties". This is why you can not use script or property binding for these roles (otherwise you get the error ListElement: cannot use script for property value).

Improvements are frequently requested to the Qt team but as far as I know there is no implementation yet.

One thing you can do is dynamically initialize the model instead of using fixed ListElement:

ListView {
    id: listView

    readonly property var modelElements: [
        {
            name: qsTr("Proccess: %1").arg(typeAssetProcess),
            colorCode: "grey"
        },
        {
            name: "Red",
            colorCode: "red"
        },
        {
            name: "blue",
            colorCode: "blue"
        },
        {
            name: "Green",
            colorCode: "green"
        }]

    Component.onCompleted: {
        modelElements.forEach(function(element) {
            model.append(element)
        })
    }

    width: parent.width
    height: parent.height
    model: ListModel {}
    delegate: ...
}

You can also choose to implement your own model in C++.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.