Qt/Qml:地图元素的中继器与 MapItemView [英] Qt/Qml: Repeater vs. MapItemView for map elements
问题描述
我想用来自 QAbstractListModel
的地图元素(如 MapCircle,...)填充 Qml 地图.似乎有两个 Qml 工具适用于此,MapItemView
[1] 和 Repeater
[2].Repeater
更强大(例如它允许嵌套模型) - 那么有什么理由使用 MapItemView
而不是 Repeater
?>
问候,
[1] http://doc.qt.io/qt-5/qml-qtlocation-mapitemview.html
[2] http://doc.qt.io/qt-5/qml-qtquick-repeater.html
MapItemView 来源:http://code.qt.io/cgit/qt/qtlocation.git/tree/src/location/declarativemaps/qdeclarativegeomapitemview.cpp
中继源:http://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/quick/items/qquickrepeater.cpp
为此,您应该使用 MapItemView
.使用 Repeater
仅在您创建 Map
时才有效,如果您之后在模型中添加元素,则不会向 Map 添加任何委托.
它最初与 Repeater
一起工作但之后不工作的事实是因为:
Repeater
将他的委托父级给他的父级,也就是Map
Map
对象然后在创建时扫描其子项一次(在 C++ 函数中相当于Component.onCompleted
)- 在此扫描后,
MapItem
派生对象的子对象将添加到地图中,就像手动调用Map.addMapItem()
- 之后由
Repeater
创建的委托只是Map
的父级,但并未真正添加"到其中.
由于 MapItemView
知道 Map
,它可以在创建时将委托添加到 Map
.
MapItemView
的一个限制是它只适用于 QAbstractItemModel
和派生的.这意味着它可以与 ListModel
或 c++ 模型一起使用,但不能与哑"模型(如 js 数组或整数作为模型)一起使用.
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
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 callingMap.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.
这篇关于Qt/Qml:地图元素的中继器与 MapItemView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!