Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Model/View: using single view and single model with several sets of data
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    morte
    wrote on last edited by
    #1

    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
    @

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsprenkle
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        morte
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jsprenkle
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved