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. ListModel recursively modifies JS arrays of appended objects - bug or feature?

ListModel recursively modifies JS arrays of appended objects - bug or feature?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 2.3k Views
  • 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.
  • F Offline
    F Offline
    Frank Gaebler
    wrote on last edited by
    #1

    The code below shows that JS objects appended to a ListModel
    are modified such that ALL arrays, that are defined in the object
    are converted to ListModels itself. So it defines somehow a tree of ListModels.
    Is this a feature or a bug? If a feature: what is the logic behind this?

    Thanks for feedback!

    Frank
    

    @
    import Qt 4.7

    Rectangle {
    ListModel {
    id: model
    }
    Component.onCompleted: {
    var itemIn = {
    foo: [1,2,3]
    };
    model.append(itemIn);
    var itemOut = model.get(0);
    console.log("itemIn.foo:", itemIn.foo, "itemOut.foo:", itemOut.foo);
    }
    }
    @

    -> output: itemIn.foo: 1,2,3 itemOut.foo: QDeclarativeListModel(0x1022a7830)

    1 Reply Last reply
    0
    • B Offline
      B Offline
      blam
      wrote on last edited by
      #2

      This is done by design. Any list item added to a ListModel is taken to be a "sub-list model".

      This allows any list within a list model to also be used as a model, which is useful for creating views for such sub-models. For example:

      @

      import Qt 4.7

      Rectangle {
      width: 200; height: 200

      ListModel { id: model }
      
      ListView {
          model: model
          anchors.fill: parent
          delegate: Column {
              Repeater {
                  model: outer
                  Text { text: inner }
              }
          }
      }
      
      Component.onCompleted: {
          model.append({'outer': [ {'inner': 'aaa'}, {'inner': 'bbb'}]})
          model.append({'outer': [ {'inner': 'ccc'}, {'inner': 'ddd'}]})
      }
      

      }
      @

      The inner view can use 'outer' as a list model in order to render the 'inner' values.

      However, the fact that a list like "[1,2,3]" can be added as an item is a bug. It should only allow lists of objects to be added. I've added a link to this post in the relevant bug report (http://bugreports.qt.nokia.com/browse/QTBUG-13640).

      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