0
votes

I want to populate a Qml Map with map elements (like MapCircle, ...) from a QAbstractListModel. There seem to be two Qml tools suitable for this, MapItemView [1] and Repeater [2]. The Repeater is more powerful (e.g. it allows nested models) - so is there any reason to use the MapItemView instead of a Repeater?

Regards,

[1] http://doc.qt.io/qt-5/qml-qtlocation-mapitemview.html

[2] http://doc.qt.io/qt-5/qml-qtquick-repeater.html

MapItemView source: http://code.qt.io/cgit/qt/qtlocation.git/tree/src/location/declarativemaps/qdeclarativegeomapitemview.cpp

Repeater source: http://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/quick/items/qquickrepeater.cpp

1
It is hard to judge from the documentation, but in most cases Views only instantiate as much as needed. So what is outside the viewport won't clutter your memory and initial creation time will be better. The Repeater is dump. It will create Itemss for every element in the model, no matter whether they will be shown or not.derM
Good point. I checked the source code: MapItemView adds all items to the Map. I am not sure what Repeater exactly does but probably it will also add the items just to the map. The map itself handles the visible item rendering.Hyndrix

1 Answers

3
votes

You should use MapItemView for that. Using Repeater works only when you create the Map, if you add elements in your model afterwards, no delegate will be added to the Map.

The fact that it works at first with Repeater but not afterwards is because:

  • the Repeater parents his delegate to his parent which is the Map
  • The Map object then scans its child items once when it's created (in a c++ function equivalent to Component.onCompleted)
  • Upon this scan the children that are MapItem-derived objects are added to the map like when manually calling Map.addMapItem()
  • Delegates that are created after that by the Repeater are just parented to the Map but not really "added" to it.

Since MapItemView is aware of the Map it can add the delegates to the Map when it creates them.

One of the limitation of MapItemView is that it only works with QAbstractItemModel and derived. That means it can work with a ListModel or a c++ model, but not with a "dumb" model like a js array or an integer as a model.