Qt/Qml:地图元素的中继器与 MapItemView [英] Qt/Qml: Repeater vs. MapItemView for map elements

查看:177
本文介绍了Qt/Qml:地图元素的中继器与 MapItemView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用来自 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 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.

这篇关于Qt/Qml:地图元素的中继器与 MapItemView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆