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();
        }
    
    
    }
  • 相关阅读:
    CSS Friendly Control Adapters CSSFriendly.dll
    2008秋季计算机软件基础未交实验报告名单
    About NeatHtml™ Brettle.Web.NeatHtml.dll
    What is DotNetOpenMail DotNetOpenMail.dll
    What is the simplest way to distribute a .NET COM server to any platform?
    页面压缩 Enabling Gzip and Deflate HTTP Compression in ASP.NET pages(转)
    [算法分析]计数排序
    [置顶] EJDesktop开源项目
    继承初体验
    [置顶] 基于stm32f103zet6之UC/OS_II的学习1(初步移植OS点灯大法)
  • 原文地址:https://www.cnblogs.com/kabe/p/9496679.html
Copyright © 2011-2022 走看看