来自C ++模型的QML MapPolygon [英] QML MapPolygon from C++ model
问题描述
我想在QML Map应用程序中动态添加/删除/编辑MapPolygon
.我还有其他一些创建多边形的工作(文件导出/导入等),所以我认为我应该在C ++模型的多边形数据中使用MapItemView
.
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.
我试图用自己的基于QObject的对象创建自己的模型:
I tried to create my own model with my own QObject based objects:
对象:
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;
};
型号:
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;
};
和QML:
MapItemView {
id: AreaView
delegate: AreaPolygon {
path: coordinates
}
model: cppPolygonModel
}
AreaPolygon.qml
AreaPolygon.qml
MapPolygon {
id: areaPolygon
border.width: 1
border.color: "red"
color: Qt.rgba(255, 0, 0, 0.1)
}
但是不幸的是,多边形没有出现在地图上(当坐标成功添加到对象QList属性中时).我认为从视图中看不到Object QList附加组件,因此MapItemView不会刷新.
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.
有没有更好的选择呢?也许我应该使用QGeoPolygon
对象的模型? (如何?)
Is there any better option to do that? Maybe I should use model of QGeoPolygon
objects? (How?)
推荐答案
您必须返回QVariantList
而不是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;
}
}
这篇关于来自C ++模型的QML MapPolygon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!