AbstractListModel rows added but QML View not being updated

457 views Asked by At

I have a model that have a list of MarkerItem (which is a struct).

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

In my QML I have

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: []
        }
    }
}

I am passing this model to QML view

_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();

The first two points that are added before setting the model to QML context appears in the view. However when I am adding some new points (based on user input from Toolbar Action) with the addMarker function It adds the markers in the model, but view does not update

All the codes in the project is uploaded on the gist

1

There are 1 answers

2
eyllanesc On BEST ANSWER

The problem can be said that it is in your code or is in your data, what happens is that the data you are giving back is in the geographic coordinates.

If you want to keep your data you only have to change the following:

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

If you do not want to change your code, exchange the coordinates in your data.

I chose the first option and moving the map a bit I got the following:

enter image description here