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

遇到两个崩溃的问题。

1、A线程中给赋值了变量 listA, 线程B中使用函数Add(QList<GeoPath> &list),由于在其函数中调用了list.at(index),所以当listA对象改变时会使得引用而来的list导致索引越界。

2、MapPolyline 中的path 可以用setPath 进行赋值,其参数类型是QGeoPath,但是使用model模式不能用QList<QGeoPath> mpath来传递值,只能用

QVariantList mpath,而mapPolyline 不是委托对象时,可以调用 QGeoPath fun()的函数直接给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 void  Add(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++调用 添加数据
void  PathModel::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");
     return  d;
 }

//虚函数     获取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);
    }
}

 代码优化

void PathModel::Add(QList<GeoPath> list)
{
     int count = list.count();
     beginResetModel();
     m_datas.clear();
     m_datas.append(list);
     beginInsertRows(QModelIndex(), 0, count-1);
     endResetModel();
}

void PathModel::clear()
{
    int count =rowCount();
    if(count>0)
    {
        beginRemoveRows(QModelIndex(), 0, count-1);
        m_datas.clear();
        endRemoveRows();
    }
 
 
}

转载于:https://www.cnblogs.com/kabe/p/9496679.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值