1. 程式人生 > >在qml中使用model給委托對象MapPolylIne的path屬性賦值。

在qml中使用model給委托對象MapPolylIne的path屬性賦值。

con num clas mod 代碼 pla eat quic class

遇到兩個崩潰的問題。

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賦值。

但是更改成

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

就沒問題。

代碼如下:

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);
    }
}

在qml中使用model給委托對象MapPolylIne的path屬性賦值。