添加了 AbstractListModel 行但未更新 QML 视图
AbstractListModel rows added but QML View not being updated
我有一个模型,其中包含 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
函数添加一些新点(基于来自 Toolbar Action 的用户输入)时,它会在模型中添加标记,但视图不会更新
项目中所有代码都上传到gist
问题可以说是在你的代码中或者是在你的数据中,发生的情况是你返回的数据是在地理坐标中。
如果您想保留您的数据,只需更改以下内容:
coordinate: QtPositioning.coordinate(position.y, position.x)
如果您不想更改代码,请交换数据中的坐标。
我选择了第一个选项并稍微移动了地图我得到了以下信息:
我有一个模型,其中包含 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
函数添加一些新点(基于来自 Toolbar Action 的用户输入)时,它会在模型中添加标记,但视图不会更新
项目中所有代码都上传到gist
问题可以说是在你的代码中或者是在你的数据中,发生的情况是你返回的数据是在地理坐标中。
如果您想保留您的数据,只需更改以下内容:
coordinate: QtPositioning.coordinate(position.y, position.x)
如果您不想更改代码,请交换数据中的坐标。
我选择了第一个选项并稍微移动了地图我得到了以下信息: