添加了AbstractListModel行,但未更新QML视图

时间:2017-09-09 13:49:00

标签: qt qml qtquick2 qtlocation

我的模型有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上下文之前添加的前两个点出现在视图中。但是,当我使用addMarker函数添加一些新点(基于来自工具栏操作的用户输入)时,它会在模型​​中添加标记,但视图不会更新

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

1 个答案:

答案 0 :(得分:0)

问题可以说是在您的代码中或在您的数据中,所发生的是您回馈的数据位于地理坐标中。

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

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

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

我选择了第一个选项并稍微移动地图我得到了以下内容:

enter image description here