Qt/Qml: Repeater vs. MapItemView for map elements

北城余情 提交于 2019-12-25 10:29:09

问题


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:


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.



来源:https://stackoverflow.com/questions/45550033/qt-qml-repeater-vs-mapitemview-for-map-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!