遇到两个崩溃的问题。

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:
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=
};
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=;index<count;index++)
{
beginRemoveRows(QModelIndex(), , );
m_datas.removeAt();
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() < || 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 =;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();
    }

}

在qml中使用model给委托对象MapPolylIne的path属性赋值。的更多相关文章

  1. 在 QML 中使用 C++ 类和对象

    Qt Quick 技术的引入,使得你能够快速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的,也有很多局限性,原来 Qt 的一些技术,比如低阶的网络编程如 QTcpSocket ...

  2. QT之在QML中使用C++类和对象

    QML其实是对ECMAScript的扩展,融合了Qt object系统,它是一种新的解释性语言,QML引擎虽然由Qt C++实现,但QML对象的运行环境说到底和C++对象的上下文环境是不通的,是平行的 ...

  3. JQuery中formSerialize()报错:对象不支持"formSerialize"属性或方法

    formSerialize()是jQuery的表单插件中提供的方法. formSerialize()的核心方法是:$.param(data); Form表单经过formSerialize(),被序列化 ...

  4. vue中,怎么给data对象添加新的属性?(尼玛这面试题居然让我给碰上了。。。。)

    Vue中给data中的对象属性添加一个新的属性时会发生什么,如何解决? 示例: <template> <div> <ul> <li v-for="v ...

  5. 应该是实例化对象的没有对属性赋值时,自动赋值为null,但不是空指针对象引用

    此时会输出两个null. Users类的实例是myUsers,但是由于javabean的作用范围是page,所以前面页面传送的javabean的设置的属性全部不能接收到.所以对象myUsers属性为自 ...

  6. 对象Transform,对属性赋值

    private void ContructRequest(Dictionary<string, string> dictionary, CustomerSearchRequest requ ...

  7. IE浏览器提示对象不支持“append”属性或方法

    如下代码在IE浏览器中无法执行,提示对象不支持“append”属性或方法 var tImg = document.createElement("img"); tImg.setAtt ...

  8. 九、Spring中使用@Value和@PropertySource为属性赋值

    首先回顾下在xml中我们是如何为spring的bean进行属性赋值呢? 大体是这样的 <bean id="person" class="com.atguigu.be ...

  9. [转载]震惊!QWidget竟然可以嵌入到QML中,QMl窗口句柄竟然是这样获取

      背景 记得在初学qml时,就被大佬告知Qml的实现有两种方式“view+item”和“engine+widow”,那么能不能将QWidget嵌入到QML中来呢,我收到的答案是不可以,原因是QML的 ...

随机推荐

  1. 天梯赛 L3-002. 堆栈

    思路:这里的线段树维护一个区间里面出现数的个数,对于Pop,push单点更新一下就好. #include<stdio.h> #include<iostream> #includ ...

  2. wannafly 挑战赛10 小H和密码

    题意:中文题就不解释了 题解: dp[i][j]表示前i 个轮盘 和一个字符串前j 个字符的匹配情况 ,具体的状态转移解释见代码 #include <cstdio> #include &l ...

  3. iOS - 图片模糊效果实现

    下面给大家介绍图片模糊效果的三种方法 第一种使用Core Image进行模糊 - (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFl ...

  4. iOS - 基础知识总结(OC版) 面试必看 再不看就要用swift了

    OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...

  5. iOS-CGContextRef

    图形上下文(Graphics Context)---绘制目标 需要在iOS应用程序的屏幕上进行绘制时,需要先定义一个UIView类,并实现它的drawRect:方法,当启动程序时,会先调用loadVi ...

  6. sql将查询结果的某个字段赋值给另一个字段

    Update a set a.NickName=b.name FROM AccountsInfo a, TT b where a.UserID=b.userId 必须要有关联的两个表

  7. 在pivotal cloud foundry上申请账号和部署应用

    Created by Wang, Jerry, last modified on Jul 04, 2016 URL: http://run.pivotal.io/ maintain your mobi ...

  8. Flutter——Radio组件、RadioListTile组件(单选按钮组件)

    Radio组件 Radio组件的常用属性: 属性 描述 value 单选的值 onChanged 改变时触发 activeColor 选中的颜色.背景颜色 groupValue 选择组的值 impor ...

  9. 基于fastai的分类网络

    classify.fastai Classifier based on fastai 欢迎访问:https://github.com/pprp/classify.fastai/ 字符识别分类器-基于f ...

  10. HDU 6187 Destroy Walls (思维,最大生成树)

    HDU 6187 Destroy Walls (思维,最大生成树) Destroy Walls *Time Limit: 8000/4000 MS (Java/Others) Memory Limit ...