QML MapPolygon from C++ model

2.2k views Asked by At

I want to dynamically add/remove/edit MapPolygon in QML Map application. I have some others jobs with created polygons (file export/import etc.) so I think that I should use MapItemView with C++ model sotirng Polygons data.

I tried to create my own model with my own QObject based objects:

Object:

class MODELSHARED_EXPORT Polygon : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<QGeoCoordinate> coordinates READ coordinates WRITE setCoordinates NOTIFY coordinatesChanged)

public:
    explicit Polygon(QObject *parent = nullptr);

    QList<QGeoCoordinate> coordinates() const;

    void setCoordinates(QList<QGeoCoordinate> coordinates);
signals:
    void coordinatesChanged(QList<QGeoCoordinate> coordinates);

public slots:
    void addCoordinate(const QGeoCoordinate & coordinate);

private:
    QList<QGeoCoordinate> m_coordinates;
};

Model:

class MODELSHARED_EXPORT PolygonModel : public QAbstractListModel
{
    ...

    QVariant data(const QModelIndex &index, int role) const override
    {
        if(index.row() >= 0 && index.row() < rowCount()) {
            switch (role) {
            case CoordinatesRole:
                return QVariant::fromValue(m_data.at(index.row())->coordinates());
            }
        }

        return QVariant();
    }

public slots:
    void addArea()
    {
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_data.append(new Polygon(this));
        endInsertRows();
    }

    void addPolygonCoordinate(const QGeoCoordinate &coordinate, int index)
    {
        if(index == -1) {
            index = rowCount() - 1;
        }

        m_data.at(index)->addCoordinate(coordinate);
        dataChanged(this->index(0), this->index(rowCount() - 1));
        qDebug() << "Adding coordinate..." << coordinate;
    }

private:
    QList<Polygon*> m_data;
};

And QML:

MapItemView {
        id: AreaView
        delegate: AreaPolygon {
            path: coordinates
        }
        model: cppPolygonModel
    }

AreaPolygon.qml

MapPolygon {
    id: areaPolygon
    border.width: 1
    border.color: "red"
    color: Qt.rgba(255, 0, 0, 0.1)
}

But unfortunately polygons did not appear on map (when coordinates are succesfully added into object QList property). I think that Object QList addidion is not visible from View and so MapItemView is not refreshing.

Is there any better option to do that? Maybe I should use model of QGeoPolygon objects? (How?)

1

There are 1 answers

0
eyllanesc On BEST ANSWER

You have to return QVariantList instead of QList<QGeoCoordinate>:

if(index.row() >= 0 && index.row() < rowCount()) {
    switch (role) {
    case CoordinatesRole:
        QVariantList coorvariant;
        for(const QGeoCoordinate & coord: m_data.at(index.row())->coordinates()){
            coorvariant.append(QVariant::fromValue(coord));
        }
        return coorvariant;
    }
}

enter image description here