在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的 ...
随机推荐
- gulp做的前端代码压缩报错,揭示具体错误 信息
用gulp做的前端代码压缩,今天在打包的时候发现打包出错了.报的错误是--unable to minify JavaScript.但是至于为什么会不能打包,表示很困惑,然后通过一番搜索,最后得出了问题 ...
- linux安装imagemagick,centos安装imagemagick方法
1.安装文件格式支持库 yum install tcl-devel libpng-devel libjpeg-devel ghostscript-devel bzip2-devel freetype- ...
- shell 学习笔记4-shell内置变量命令
一.shell 的一些内置命令 常用的一内部命令有:echo.eval.exec.export.read.shift 1.echo命令-在屏幕中输出信息 1)说明 格式:echo args #< ...
- html启动本地.exe文件
之前没有了解这个内容,还是一个小伙伴在面试中遇到的一个题目,感觉挺有意思就研究了一下这个东西到底是怎么用的.搜了一下解决方法,是添加注册表,自己运行了可以使用 第一步:首先打开注册表,方法是 win+ ...
- LunHui 的生命观
LunHui 的生命观 来源 https://www.zhihu.com/question/346510295 作者:齐天大圣链接:https://www.zhihu.com/question/346 ...
- 【转载】Asp.Net MVC网站提交富文本HTML标签内容抛出异常
今天开发一个ASP.NET MVC网站时,有个页面使用到了FCKEditor富文本编辑器,通过Post方式提交内容时候抛出异常,仔细分析后得出应该是服务器阻止了带有HTML标签内容的提交操作,ASP. ...
- wampserver的使用配置
1.正常安装就不说了,只需要把安装位置改成需要的位置就可以了.其它的默认就可以了. 2.安装完成之后打开wampserver. 3.现在该修改密码了: (1)点击进入mysql控制台. (2)Wamp ...
- CVE-2019-11604 Quest KACE Systems Management Appliance <= 9.0 XSS
CVE-2019-11604 Quest KACE Systems Management Appliance CVE-2019-11604 Quest KACE Systems Management ...
- Source Insight用法
快捷键 Symbol: Browse Project Symbols-:F7, Alt+G Relation View Relationship->For Functions->Pefer ...
- 记一次靠谱的nvm在window系统的安装
最近电脑系统重装,电脑的环境配置也清空掉了,今天想写点代码,突然发现什么环境也没了,又要重新配置了. 安装node环境的话,完全可以直接去node的官网下载安装包安装,也不是特别复杂,但是由于之前跑的 ...