Lubos
Lubos

Reputation: 31

How can I show dynamic list of MapQuickItem?

How can I show for example dynamic list of planes from http://doc.qt.io/qt-5/qtlocation-planespotter-example.html example? I would like to add and remove items from the map. And also refresh each changed item. I need to show objects from QList or QAbstractItemModel on the map as MapQuickItem.

Upvotes: 2

Views: 1375

Answers (3)

Dmitry
Dmitry

Reputation: 2168

There are 2 methods for Map to manipulate items dynamically : void addMapItem(MapItem item) and void clearMapItems()

Here is a code snippet from my project( I guess rather self-documented) :

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

Upvotes: 0

Lubos
Lubos

Reputation: 31

The right answer is: use MapItemView :)

MapItemView {
    model: planeModel
    delegate: Plane { }
}

Upvotes: 1

Kevin Krammer
Kevin Krammer

Reputation: 5207

Both a QList based property or a QAbstractListModel derived class would work.

On the QML Side you would use either as the model for a Repeater that uses the Plane type as its delegate, taking the coordinate from the model handle

Somewhat like this

Map {
    Repeater {
        model: listOrModelFromCpp

        delegate: Plane {
            cooridinate: model.position // or model.modelData for a QList<QGeoCoordinate> as the listOrModelFromCpp
        }
    }
}

Using a custom QAbstractListModel derived model has the advantage that the Repeater can individually create and destroy Plane items, while a basic list will require it to recreate all items when the list's count changes

Upvotes: 0

Related Questions