Encountered two crashes.
1, a thread is assigned to the variable ListA, thread B uses the function add (qlist<geopath> &list), because in its function called the list.at (index), So when the Lista object changes, it causes the referenced list to cause the index to go out of bounds.
2. Use Beginremoverows (Qmodelindex (), 0, RowCount ()) to define the clear () function, m_datas.clear (); Endremoverows (); The program also crashes for unknown reasons.
3, the path in Mappolyline can be assigned with SetPath, its parameter type is Qgeopath, but use model mode can not use qlist<qgeopath> MPath to pass the value, only with
Qvariantlist MPath, and Mappolyline is not a delegate object, you can call the Qgeopath fun () function to assign a value directly to path.
But change to
1 int count =rowCount ();2for (int index=0;index<count; index++)3 {4 00); 5 M_datas.removeat (0); 6 endremoverows (); 7 }
There's no problem.
The code is as follows:
mapitemview{ model:situationtargetmodel delegate: Mappolyline { 1 line.color:'red' path:model.path } }
New Pathmodel (); M_quickview->rootcontext ()->setcontextproperty (" Situationtargetmodel", Psituationtargetmodel);
#ifndef geopath_h #define geopath_h< Span style= "COLOR: #000000" > #include <qgeopath> #include < Qgeocoordinate> #include <QVariantList> class geopath{ public : Geopath (); void addcoordinate (qgeocoordinate & data); Qvariantlist path () const ; private #endif // Geopath_h
" geopath.h " #include<QDebug>Geopath::geopath () {}void geopath::addcoordinate (qgeocoordinate &data) { mpath.append (qvariant::fromvalue (data));} Qvariantlist Geopath::p ath ()const{ return mpath;}
#ifndef Pathmodel_h#definePathmodel_h#include<QAbstractListModel>#include<QModelIndex>#include<QVariant>#include"geopath.h"classPathmodel: Publicqabstractlistmodel{Q_object Public: enumdatatype{Path=1 }; Pathmodel (Qobject* parent=NULL); //External interface C + + calls to add dataQ_invokablevoidADD (geopath&path); voidADD (qlist<geopath>list); //external interface Clear modelQ_invokablevoidClear (); //virtual function QML Internal call gets the content of the index row role content indexQvariant Data (ConstQmodelindex &index,intRole =QT::D isplayrole)Const; //virtual function Gets the number of model rows intRowCount (ConstQmodelindex &parent = Qmodelindex ())Const; //alias for virtual function content QML Internal callqhash<int, Qbytearray> rolenames ()Const; ~Pathmodel () {}Private: //Model data CollectionQlist<geopath>M_datas;};#endif //Pathmodel_h
#include"pathmodel.h"#include<QDebug>Pathmodel::P athmodel (Qobject*parent): Qabstractlistmodel (parent) {}//External interface C + + calls to add datavoidPathmodel::add (geopath&path) {Qdebug ()<<"Pathmodel::add" ; Begininsertrows (Qmodelindex (), RowCount (), RowCount ()); M_datas.append (path); Endinsertrows (); Qdebug ()<<m_datas.size ();}//external interface Clear modelvoidpathmodel::clear () {intCount =RowCount (); for(intindex=0; index<count;index++) {beginremoverows (Qmodelindex (),0,0); M_datas.removeat (0); Endremoverows (); }}//virtual function QML Internal call gets the content of the index row role content indexQvariant Pathmodel::d ata (ConstQmodelindex &index,intRoleConst{qdebug ()<<"Pathmodel::d ata"<<index.row () <<" Total"<<m_datas.size (); if(Index.row () <0|| Index.row () >=m_datas.size ()) { returnqvariant (); } Constgeopath& d =M_datas[index.row ()]; if(Role = =datatype::p ath) { returnQvariant::fromvalue (D.path ()); } returnqvariant ();}//alias for virtual function content QML Internal callqhash<int, Qbytearray> Pathmodel::rolenames ()Const{Qhash<int, qbytearray> d =Qabstractlistmodel::rolenames (); D.insert (datatype::p ath,"Path"); returnD;}//virtual function Gets the number of model rowsintPathmodel::rowcount (ConstQmodelindex &parent)Const{q_unused (parent); returnm_datas.size ();}voidPathmodel::add (qlist<geopath>list) { intCount =List.count (); Geopath D; for(intindex =0; index<count;index++) {D=list.at (index); ADD (d); }}
Use the model in QML to assign a value to the Path property of the delegate object Mappolyline.