ListModel javascript functor storage



  • I am still quite new to qt/qml and javascript so this might be newbie question. I have a situation where I have an application that has data stored in a ListModel, when the ListView that owns the model updates an item in the model I want to call a functor that will update the value in a couple places, but every time I try to run the functor stored in the model I get a type error.

    @
    qrc:///main.qml:50: TypeError: Type error
    qrc:///main.qml:50: TypeError: Type error
    qrc:///main.qml:50: TypeError: Type error
    @

    Here is a demo of the issue. I created it using default Qt Quick application in Qt Creator. I am using Qt 5.3.1.

    @
    import QtQuick 2.2
    import QtQuick.Window 2.1

    Window {
    visible: true
    width: 360
    height: 360

    property int value1: 5
    property int value2: 6
    property int value3: 7
    
    Component.onCompleted: {
        function Func(n) {
            return function(i) {
                value1 = i;
                return "This is cell: " + i + " and is associated to property that had value: " + n;
            }
        };
    
        view.model.append( { func : new Func(value1) } );
        view.model.append( { func : new Func(value2) } );
        view.model.append( { func : new Func(value3) } );
    }
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    
    ListView {
        id: view
        anchors.fill: parent
    
        model: ListModel {}
        delegate: Component {
    
            Rectangle {
                width: view.width
                height: 25
                color: "red"
                border.color: "black"
    
                Text {
                    id: test
                    text: {
                        var test = model.func;
                        test(index);
                    }
                }
            }
        }
    }
    

    }

    @

    There might be better ways to do this, but I would still like to know what I am doing wrong. Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.