zoukankan      html  css  js  c++  java
  • 在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. 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();
        }
    
    
    }
  • 相关阅读:
    pat甲级 1155 Heap Paths (30 分)
    pat甲级 1152 Google Recruitment (20 分)
    蓝桥杯 基础练习 特殊回文数
    蓝桥杯 基础练习 十进制转十六进制
    蓝桥杯 基础练习 十六进制转十进制
    蓝桥杯 基础练习 十六进制转八进制
    51nod 1347 旋转字符串
    蓝桥杯 入门训练 圆的面积
    蓝桥杯 入门训练 Fibonacci数列
    链表相关
  • 原文地址:https://www.cnblogs.com/kabe/p/9496679.html
Copyright © 2011-2022 走看看