Qt 如何显示MapQuickItem的动态列表?
如何显示示例中的动态平面列表? 我想在地图上添加和删除项目。并刷新每个更改的项目。Qt 如何显示MapQuickItem的动态列表?,qt,model,qml,Qt,Model,Qml,如何显示示例中的动态平面列表? 我想在地图上添加和删除项目。并刷新每个更改的项目。 我需要在地图上将QList或QAbstractItemModel中的对象显示为MapQuickItem。基于QList的属性或QAbstractListModel派生的类都可以工作 在QML端,您可以将其中一个用作中继器的模型,该中继器使用平面类型作为其委托,从模型句柄获取坐标 有点像这样 Map { Repeater { model: listOrModelFromCpp
我需要在地图上将QList或QAbstractItemModel中的对象显示为MapQuickItem。基于QList的属性或QAbstractListModel派生的类都可以工作 在QML端,您可以将其中一个用作
中继器的模型,该中继器使用平面
类型作为其委托,从模型
句柄获取坐标
有点像这样
Map {
Repeater {
model: listOrModelFromCpp
delegate: Plane {
cooridinate: model.position // or model.modelData for a QList<QGeoCoordinate> as the listOrModelFromCpp
}
}
}
Map{
中继器{
型号:listOrModelFromCpp
代表:飞机{
cooridinate:model.position//或model.modelData作为listOrModelFromCpp的QList
}
}
}
使用自定义的QAbstractListModel
派生模型的优点是中继器可以单独创建和销毁平面
项,而基本列表将要求它在列表计数更改时重新创建所有项正确答案是:使用MapItemView:)
Map有两种方法可以动态操作项目:
void addMapItem(MapItem项)
和void clearMapItems()
下面是我的项目中的一段代码片段(我想应该是自我记录的):
函数clearTargets()
{
clearMapItems();
}
函数showPlaneItems(planeItemsToShow)
{
对于(var idx=0;idx
MapItemView {
model: planeModel
delegate: Plane { }
}
function clearTargets()
{
map.clearMapItems();
}
function showPlaneItems( planeItemsToShow )
{
for ( var idx = 0; idx < planeItemsToShow.length; idx++ ) {
var item = planeItemsToShow[idx];
var itemComponent = Qt.createComponent("qrc:/components/Plane.qml");
if ( itemComponent.status == Component.Ready ) {
var itemObject = itemComponent.createObject( map,
{
"planeName" : item["targetName"],
"coordinate" : QtPositioning.coordinate(item["targetLattitude"],
item["targetLongitude"] )
}
);
if ( itemObject != null ) {
map.addMapItem( itemObject );
}
}
}
}