QML MapPolygon from C++ model

我的未来我决定 提交于 2020-02-24 06:37:28

问题


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:


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



来源:https://stackoverflow.com/questions/51428077/qml-mappolygon-from-c-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!