在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的 ...
随机推荐
- Linux 内核错误码
#define EPERM 1 /* Operation not permitted */#define ENOENT 2 /* No such ...
- 怎样安装ipython
ipython 是一个python的交互式shell, 比默认的python shell更好用, 支持自动补全 / 上下翻等功能. 下面是按照方法: # 通用安装方法 pip install ipy ...
- Powershell学习笔记:(二)、基础知识
从Window7以后,WIndows系统都自带了Windows PowerShell. 自带版本如下 WIndow7 2.0 WIndow8 3.0 Window8.1 4.0 Win ...
- C#ModBus Tcp 报文解析
上一篇博客已经完成 C#ModBus Tcp Master的实现 本篇主要对不同的功能码所发出的报文进行解析(包括请求报文及响应报文) 读操作 功能码 0x01 读一组线圈 读取站号为1 从地址12开 ...
- React Native 开发豆瓣评分(一)环境搭建&配置模拟器
详细可参考 官方文档,这里进记录一些重要过程. 安装环境 下载 Android Studio 选择 Custom 进行安装: Android SDK Android SDK Platform Perf ...
- iOS 内存管理的一点小问题
现在大家的项目应该基本都是ARC了,如果还是MRC的话,赶紧转换到ARC吧!最近被临时拉过去开发iPad,由于项目原因,还是使用的MRC.今天在调部分界面的时候,发现一段代码,我怎么看都怎么觉得怪怪的 ...
- 华为OJ:字符串处理
#include <iostream> #include <stdlib.h> #include <string> #include <sstream> ...
- "=="和equals小结
通俗点讲,==是看看左右是不是一个东西.equals是看看左右是不是长得一样.如何记住嘛.如果单纯是想记住, ==:等于.equals:相同.两个长得一样的人,只能说长的相同(equals),但是不 ...
- vulkan load store and memoryless
https://www.jendrikillner.com/article_database/ https://community.arm.com/developer/tools-software/g ...
- vue-resouce设置请求头