在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的 ...
随机推荐
- CentOS 6.9安装配置nmon
nmon是一款开源的性能监控工具,用于监控CentOS系统的资源消耗信息,并能把结果输出到文件中,然后通过nmon_analyser性能报告分析器生成数据分析报表. 一.安装nmon: 1. 配置ep ...
- 音视频入门-05-RGB-TO-BMP使用开源库
* 音视频入门文章目录 * RGB-TO-BMP 回顾 将 RGB 数据转成 BMP 图片: 了解 BMP 文件格式 准备 BMP 文件头信息 准备 BMP 信息头 BMP 存储 RGB 的顺序是 B ...
- 2019年全国高校sql数据库
2019年全国高校名单sql数据库信息: 根据教育部发布的文件整理形成可用.sql文件,导入数据库即可使用. 包括全国高等学校共计2956所,其中:普通高等学校2688所(含独立学院257所),成人高 ...
- 【转】Java中File常用的方法汇总
创建:createNewFile()在指定位置创建一个空文件,成功就返回true,如果已存在就不创建,然后返回false.mkdir() 在指定位置创建一个单级文件夹.mkdirs() 在指定位置创建 ...
- 关于Ad-hoc
Ad-hoc是wifi的一个模式,依托普通无线局域网802.11家族.网络中所有结点的地位平等,无需设置任何的中心控制结点,省去了无线中介设备AP.比如一台电脑建立了一个网络,这个时候加入了两台电脑. ...
- 【leetcode】544. Output Contest Matches
原题 During the NBA playoffs, we always arrange the rather strong team to play with the rather weak te ...
- Oracle存储过程、函数、包加密wrap
wrap加密可以将PL/SQL的代码实现部分隐藏,提高代码的安全性,如存储过程.函数.包等都隐藏. wrap加密的方法有两种,下面以函数为例分别介绍一下: 方法一: 编写好函数后保存到 d:\test ...
- 解决windows 1903 alt + tab 切换卡顿
右击此电脑图标 选择管理 服务和应用程序 服务 禁用system interface foundation service
- servlet版本与tomcat版本对应关系,各版本web.xml头信息写法
The mapping between the specifications and the respective Apache Tomcat versions is: Servlet Spec JS ...
- java相关网址汇总(myself)
jar包下载网址 https://www.mvnjar.com/ 或者 https://mvnrepository.com/ 或者 http://www.java2s.com/Open-Source/ ...