Model/View: using single view and single model with several sets of data



  • I want to have single instance of ListView, single instance of ListModel and one delegate to display several different sets of data (with different roles). Im trying to implement stuff in that way to compact and unite code.

    UPDATE: i change code (delegate accesses data from model via get method instead of role properties) and now it works, but still drops strange messages to console.

    Here new code snippet.

    @
    import QtQuick 2.2

    Rectangle {
    id: main
    width: 640; height: 480

    property string mode: "letters"

    ListView {
    id: view
    anchors.top: main.top; anchors.bottom: button.top
    delegate: delegate
    model: model1
    }

    Component {
    id: delegate
    Text {
    text: {
    main.mode == "letters" ? model1.get(index).letter : model1.get(index).number;
    }
    }
    }

    ListModel {
    id: model1
    Component.onCompleted: {
    model1.append({"letter":"A"});
    model1.append({"letter":"B"});
    model1.append({"letter":"C"});
    }
    }

    Rectangle {
    id: button
    anchors.bottom: main.bottom; width: 100; height: 100; border.width: 1

    MouseArea {
    anchors.fill: parent
    Text {
    anchors.centerIn: parent
    text: "Change"
    }

    onClicked: {
    model1.clear();
    mode = "numbers";
    model1.append({"number":"1"});
    model1.append({"number":"2"});
    model1.append({"number":"3"});
    }
    }
    }
    }
    @

    Here the application output:

    @
    Starting C:\Qt\5.3.0\5.3\mingw482_32\bin\qmlscene.exe...
    file:///C:/workspace/test/test.qml:24: TypeError: Cannot read property 'letter' of undefined
    file:///C:/workspace/test/test.qml:24: TypeError: Cannot read property 'letter' of undefined
    file:///C:/workspace/test/test.qml:24: TypeError: Cannot read property 'letter' of undefined
    file:///C:/workspace/test/test.qml:24: TypeError: Cannot read property 'number' of undefined
    file:///C:/workspace/test/test.qml:24: TypeError: Cannot read property 'number' of undefined
    file:///C:/workspace/test/test.qml:24: TypeError: Cannot read property 'number' of undefined
    @



  • it's complaining about this:

    @ if (mode == "numbers")
    number
    @

    It's interpreting this as javascript. The word "number" by itself isn't valid javascript. Are you trying to change the text displayed?



  • [quote author="jsprenkle" date="1401913206"]it's complaining about this:

    @ if (mode == "numbers")
    number
    @

    It's interpreting this as javascript. The word "number" by itself isn't valid javascript. [/quote]

    You mean

    @ if (mode == "numbers")
    number
    @

    syntactically incorrect? "number" should be valid in delegate scope like "letter" valid.

    bq. Are you trying to change the text displayed?

    Im trying to display data, then when an even happens load new data in same model and display it with existent view.



  • I think it's reading it as javascript not as part of the delegate. I think you might want to change the role used by the delegate by doing something like:

    styleData.role = "number"

    This seems to not be very well documented. I inferred this from these two sources:

    http://stackoverflow.com/questions/22874387/qml-tableview-access-model-properties-from-delegate

    http://qt-project.org/doc/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop


Log in to reply
 

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