Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何显示MapQuickItem的动态列表?_Qt_Model_Qml - Fatal编程技术网

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 );
         }
      }
   }
}