在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:
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属性赋值。的更多相关文章
- 在 QML 中使用 C++ 类和对象
Qt Quick 技术的引入,使得你能够快速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的,也有很多局限性,原来 Qt 的一些技术,比如低阶的网络编程如 QTcpSocket ...
- QT之在QML中使用C++类和对象
QML其实是对ECMAScript的扩展,融合了Qt object系统,它是一种新的解释性语言,QML引擎虽然由Qt C++实现,但QML对象的运行环境说到底和C++对象的上下文环境是不通的,是平行的 ...
- JQuery中formSerialize()报错:对象不支持"formSerialize"属性或方法
formSerialize()是jQuery的表单插件中提供的方法. formSerialize()的核心方法是:$.param(data); Form表单经过formSerialize(),被序列化 ...
- vue中,怎么给data对象添加新的属性?(尼玛这面试题居然让我给碰上了。。。。)
Vue中给data中的对象属性添加一个新的属性时会发生什么,如何解决? 示例: <template> <div> <ul> <li v-for="v ...
- 应该是实例化对象的没有对属性赋值时,自动赋值为null,但不是空指针对象引用
此时会输出两个null. Users类的实例是myUsers,但是由于javabean的作用范围是page,所以前面页面传送的javabean的设置的属性全部不能接收到.所以对象myUsers属性为自 ...
- 对象Transform,对属性赋值
private void ContructRequest(Dictionary<string, string> dictionary, CustomerSearchRequest requ ...
- IE浏览器提示对象不支持“append”属性或方法
如下代码在IE浏览器中无法执行,提示对象不支持“append”属性或方法 var tImg = document.createElement("img"); tImg.setAtt ...
- 九、Spring中使用@Value和@PropertySource为属性赋值
首先回顾下在xml中我们是如何为spring的bean进行属性赋值呢? 大体是这样的 <bean id="person" class="com.atguigu.be ...
- [转载]震惊!QWidget竟然可以嵌入到QML中,QMl窗口句柄竟然是这样获取
背景 记得在初学qml时,就被大佬告知Qml的实现有两种方式“view+item”和“engine+widow”,那么能不能将QWidget嵌入到QML中来呢,我收到的答案是不可以,原因是QML的 ...
随机推荐
- 撸一个 vue 的截图组件,按比例截取
<template> <div class="clip-img" :style="imgStyle"> <img :src=&qu ...
- Spring Cloud Alibaba学习笔记(5) - 整合Sentinel及Sentinel规则
整合Sentinel 应用整合Sentinel 在dependencies中添加依赖,即可整合Sentinel <dependency> <groupId>com.alibab ...
- docker registry-v2 搭建私有仓库
参考官方文档:https://docs.docker.com/registry/deploying/ 参考 :http://www.tuicool.com/articles/6jEJZj 本例子使用两 ...
- 雷达无线电系列(三)经典CFAR算法门限因子alpha计算(matlab)
前言 本文汇集CA.SO.GO.OS.杂波图等恒虚警算法的门限因子求解方法及其函数 1,CA-CFAR [非常简单,可以直接求解] %% 均值恒虚警_门限因子计算公式 %% 版本:v1 %% 时间:2 ...
- 【SQL Server DBA】维护语句:删除并创建外键约束、获取建表语句
原文:[SQL Server DBA]维护语句:删除并创建外键约束.获取建表语句 1.删除外键约束,建立外键约束 先建立3个表: /* drop table tb drop table tb_b dr ...
- 2019年全国高校sql数据库
2019年全国高校名单sql数据库信息: 根据教育部发布的文件整理形成可用.sql文件,导入数据库即可使用. 包括全国高等学校共计2956所,其中:普通高等学校2688所(含独立学院257所),成人高 ...
- VBA分别使用MSXML的DOM属性和XPATH进行网页爬虫
本文要重点介绍的是VBA中的XmlHttp对象(MSXML2.XMLHTTP或MSXML.XMLHTTP),它可以向http服务器发送请求并使用微软XML文档对象模型Microsoft XML Doc ...
- 艾瑞克·弗洛姆 ( Erich Fromm )
艾瑞克·弗洛姆 ( Erich Fromm ) 来源 https://www.zhihu.com/question/22891103 ------------------------------ 作者 ...
- wstngfw中配置freeradius
wstngfw中配置freeradius Radius为各种网络设备和服务提供了一个认证来源. Radius认证常用于***.入网门户.交换机.路由器和防火墙.Radius认证比在网络上的不同设备跟踪 ...
- H5表单新特性
1.HTML5表单新特性之——新的input type <input type=" "> HTML5之前已有的input type: text.password.rad ...