问题
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 theMap
- The
Map
object then scans its child items once when it's created (in a c++ function equivalent toComponent.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 theMap
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