0

我有一个模型,它有一个列表MarkerItem(这是一个结构)。

struct MarkerItem{
    enum marker_state{
        marker_observation = 0,
        marker_important,
        marker_redundant,
        marker_deleted
    };
    MarkerItem(const QPointF& pos, marker_state state, const QDateTime& when, const QString& label);

    const QPointF& position() const;
    QGeoCoordinate coordinate() const;
    const QString& label() const;
    marker_state state() const;

    void change_state(marker_state state);

  private:
    QPointF      _position;
    marker_state _state;
    QString      _label;
    QDateTime    _when;
};
class MarkerModel : public QAbstractListModel{
  Q_OBJECT
  Q_PROPERTY(QGeoRoute* route READ route NOTIFY routeChanged)
  public:
    enum MarkerRoles {
        PositionRole = Qt::UserRole + 1,
        StateRole,
        LabelRole
    };
    explicit MarkerModel(QObject *parent = 0);
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
  public:
    QHash<int, QByteArray> roleNames() const;
  private:
    QList<MarkerItem*> _markers;
  public:
    void addMarker(MarkerItem* marker);
  public:
    QGeoRoute* route() const;
  signals:
    void routeChanged();
};

void MarkerModel::addMarker(MarkerItem *marker){
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    _markers.push_back(marker);
    qWarning() << rowCount();
    endInsertRows();
}

在我的 QML 中,我有

Map {
    id: map
    anchors.fill: parent
    plugin: mapPlugin
    center: QtPositioning.coordinate(22.5726, 88.3639)
    zoomLevel: 14

    MapItemView {
        model: markerModel
        delegate: markerDelegate
    }

    Component {
        id: markerDelegate

        MapQuickItem{
            anchorPoint: Qt.point(2.5, 2.5)
            coordinate: QtPositioning.coordinate(position.x, position.y)
            zoomLevel: 0
            sourceItem: Rectangle{
                width:  settings.marker_size;
                height: settings.marker_size;
                radius: settings.marker_size/2;
                color:  settings.marker_colors[status]
                border.color: "white"
                border.width: 1
            }
        }
    }
    Component{
        id: polyline

        MapPolyline {
            line.color: black
            line.width: 2
            path: []
        }
    }
}

我将此模型传递给 QML 视图

_model->addMarker(new MarkerItem(QPointF(22.5868f, 88.4149f), MarkerItem::marker_observation, QDateTime::currentDateTime(), "1"));
_model->addMarker(new MarkerItem(QPointF(22.5391f, 88.3958f), MarkerItem::marker_observation, QDateTime::currentDateTime(), "2"));

qRegisterMetaType<MarkerModel*>("MarkerModel");
QWidget* container = QWidget::createWindowContainer(_view, this);
container->setFocusPolicy(Qt::TabFocus);
_view->engine()->rootContext()->setContextProperty("markerModel", _model);
_view->setSource(QUrl("qrc:///main.qml"));

QVBoxLayout* layout = new QVBoxLayout;
setLayout(layout);

layout->addWidget(container);

_root = _view->rootObject();

在将模型设置为 QML 上下文之前添加的前两个点出现在视图中。但是,当我使用该函数添加一些新点(基于Toolbar Action的用户输入)时,addMarker它会在模型​​中添加标记,但视图不会更新

项目中的所有代码都上传到gist上

4

1 回答 1

0

问题可以说是在您的代码中或在您的数据中,发生的情况是您返回的数据在地理坐标中。

如果您想保留您的数据,您只需更改以下内容:

coordinate: QtPositioning.coordinate(position.y, position.x)

如果您不想更改代码,请交换数据中的坐标。

我选择了第一个选项,稍微移动一下地图,我得到了以下信息:

在此处输入图像描述

于 2017-09-09T21:18:17.217 回答