在qml中使用model给委托对象MapPolylIne的path属性赋值。

胸怀万里世界, 放眼无限未来。这篇文章主要讲述在qml中使用model给委托对象MapPolylIne的path属性赋值。相关的知识,希望能为你提供帮助。
遇到两个崩溃的问题。
1、A线程中给赋值了变量 listA, 线程B中使用函数Add(QList< GeoPath> & list),由于在其函数中调用了list.at(index),所以当listA对象改变时会使得引用而来的list导致索引越界。
2、 定义clear()函数时使用  beginRemoveRows(QModelIndex(), 0, rowCount()); m_datas.clear(); endRemoveRows(); 程序也崩溃,原因不明。
3、MapPolyline 中的path 可以用setPath 进行赋值,其参数类型是QGeoPath,但是使用model模式不能用QList< QGeoPath> mpath来传递值,只能用

QVariantList mpath,而mapPolyline 不是委托对象时,可以调用 QGeoPath fun()的函数直接给path赋值。

但是更改成
1int count =rowCount(); 2for(int index=0; index< count; index++) 3{ 4beginRemoveRows(QModelIndex(), 0, 0); 5m_datas.removeAt(0); 6endRemoveRows(); 7}

【在qml中使用model给委托对象MapPolylIne的path属性赋值。】就没问题。
 
代码如下:
MapItemView{ model: situationTargetModel delegate:MapPolyline { line.width: 1 line.color:‘red‘ path:model.path} }

PathModel *pSituationTargetModel = new PathModel (); m_quickView-> rootContext()-> setContextProperty("situationTargetModel",pSituationTargetModel);

#ifndef GEOPATH_H #define GEOPATH_H#include< QGeoPath> #include< QGeoCoordinate> #include< QVariantList> class GeoPath { public: GeoPath(); void addCoordinate(QGeoCoordinate & data); QVariantList path()const; private: QVariantList mpath; }; #endif // GEOPATH_H

#include "geopath.h" #include< QDebug> GeoPath::GeoPath() {} void GeoPath::addCoordinate(QGeoCoordinate & data) { mpath.append(QVariant::fromValue(data)); }QVariantList GeoPath::path()const { return mpath; }

#ifndef PATHMODEL_H #define PATHMODEL_H #include< QAbstractListModel> #include< QModelIndex> #include< QVariant> #include"geopath.h"class PathModel : public QAbstractListModel { Q_OBJECT public: enum datatype{ path=1 }; PathModel(QObject*parent=NULL); //外部接口 C++调用 添加数据 Q_INVOKABLE voidAdd(GeoPath& path); void Add(QList< GeoPath> list); //外部接口 清除model Q_INVOKABLE void clear(); //虚函数qml内部调用获取第index行的内容role 内容索引 QVariant data(const QModelIndex & index, int role =Qt::DisplayRole) const; //虚函数获取model行数 int rowCount(const QModelIndex & parent= QModelIndex() ) const; // 虚函数 内容的别名qml 内部调用 QHash< int, QByteArray> roleNames()const; ~PathModel() {}private: //model数据集合 QList< GeoPath> m_datas; }; #endif // PATHMODEL_H

#include "pathmodel.h" #include< QDebug> PathModel::PathModel(QObject*parent) :QAbstractListModel(parent) {} //外部接口 C++调用 添加数据 voidPathModel::Add(GeoPath& path) { qDebug()< < "PathModel::Add"; beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_datas.append(path); endInsertRows(); qDebug()< < m_datas.size(); }//外部接口 清除model void PathModel::clear() { int count =rowCount(); for(int index=0; index< count; index++) { beginRemoveRows(QModelIndex(), 0, 0); m_datas.removeAt(0); endRemoveRows(); }}//虚函数qml内部调用获取第index行的内容role 内容索引 QVariant PathModel::data(const QModelIndex & index, int role) const { qDebug()< < "PathModel::data"< < index.row()< < "total"< < m_datas.size(); if (index.row() < 0 || index.row() > = m_datas.size()) { return QVariant(); } const GeoPath& d = m_datas[index.row()]; if (role == datatype::path) { return QVariant::fromValue(d.path()); } return QVariant(); }// 虚函数 内容的别名qml 内部调用 QHash< int, QByteArray> PathModel::roleNames()const { QHash< int, QByteArray> d = QAbstractListModel::roleNames(); d.insert(datatype::path,"path"); returnd; }//虚函数获取model行数 int PathModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent); return m_datas.size(); }void PathModel::Add(QList< GeoPath> list) { int count = list.count(); GeoPath d; for(int index =0; index< count; index++) { d = list.at(index); Add(d); } }

 

    推荐阅读