Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with setData from QML using QAbstractListModel



  • Hello,

    I'm using Qt Creator 4.7.1. I want to create a list model which can be integrated at QML layer. I used QAbstractListModel followed example at https://www.ics.com/files/qtdocs/declarative-modelviews-abstractitemmodel.html

    Additional, I want my list model to be editable. The instruction can be find at https://doc.qt.io/archives/qt-4.7/qabstractitemmodel.html#setData

    I have implemented setData() and flags() function, but when I edit my list model at QML layer, all I have is a warning that "Error: Cannot assign to read-only property "type""

    My QML code is simple like that:

    ListView {
        width: 200; height: 250
        anchors.fill: parent
        
        model: myModel
        delegate: Rectangle
        {
        width: 200
        height: 50
        Text { text: "Animal: " + type + ", " + size }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                type = "Fish"
            }
        }
    }
    }
    

    But when I used my code in Qt Creator 5.4 (with some modification such as roleName()), the function setData have worked successfully.

    Please help me. Thank you.


Log in to reply