This forum has been archived. All content is frozen. Please use KDE Discuss instead.

How can treemodel be updated?

Tags: None
(comma "," separated)
strag
Registered Member
Posts
11
Karma
0
OS

How can treemodel be updated?

Mon Feb 29, 2016 8:11 pm
Hello!

I am using marble within my own Qt application to show technical objects on the map. I am using the 15.08 release of marble.
I would like to create different sets of my objects and to add them to treemodel as a document to display on the map. But I don't understand, how to update treemodel with this document?
Fragments of my code:
Code: Select all
1 QMap<QString, QVector<double> > mapSt1, mapSt2;       // maps of technical objects; string - name of the object,
2                                                       //QVector<double> - coordinates {b,l,h} of this objects
3 MarbleWidget w = new MarbleWidget;
4 document = new GeoDataDocument;
5 ...
6 //adding data from mapSt1 to the treemodel
7 foreach (QString st, mapSt1.keys())
8 {
9      GeoDataPlacemark *place = new GeoDataPlacemark(st);
10     place->setCoordinate(l, b, 0, GeoDataCoordinates::Radian);
11    w->document()->append(place);
12 }
13
14  w->model()->treemodel()->addDocument(document);                                 //this works good, objects are shown on the map
15
16 //now I want to add places from mapSt2 using existing document
17 foreach (QString st, mapSt2.keys())
18 {
19      GeoDataPlacemark *place = new GeoDataPlacemark(st);
20     place->setCoordinate(l, b, 0, GeoDataCoordinates::Radian);
21    w->document()->append(place);
22 }
23
24 //version 1
25 w->update();              //nothing updates on the map
26
27 //version 2
28 w->model()->treeModel()->addDocument(document);  //this does not work, because treemodel has already contained this document
29
30 //now I want to remove data from mapSt2
31 w->model()->treeModel()->removeDocument(document);
32 document->clear();   
33 //adding data from mapSt1 to the treemodel     (lines 7-14)   
....                 


If lines 17-29 are commented, all works good. Objects from mapSt1 are added on the map and removed from the map many times (by pressing special button in my application). But if I added objects from mapSt2, the program crashes on the line 31. Can you please tell me how to solve the problem?

Excuse me for my English :)

Last edited by strag on Wed Mar 02, 2016 10:59 am, edited 1 time in total.
User avatar
Earthwings
KDE Developer
Posts
172
Karma
1
OS

Re: How can treemodel be updated?

Mon Feb 29, 2016 8:38 pm
Hi there,

the w->document() portion looks a bit odd. I assume you either mean document there, or you have created a subclass of MarbleWidget that provides this method.

The crash in line 31 likely occurs because you updated the document before removing it, so its layout has changed and does not match the one the tree model expects.

Regarding the actual update, try replacing line 28
Code: Select all
w->model()->treeModel()->addDocument(document)
with
Code: Select all
w->model()->treeModel()->updateFeature(document)
A GeoDataDocument is a GeoDataFeature, and all the add/removeDocument methods of the tree model are really just convenience methods that forward to add/removeFeature internally.
strag
Registered Member
Posts
11
Karma
0
OS

Re: How can treemodel be updated?

Mon Feb 29, 2016 9:01 pm
Thank you very much for the quick reply!

About w->document(): it's my inattention, you are right, I have created a subclass of MarbleWidget.

About the crash in line 31: thank you, I understand this.

I tried replacing line 28 with
Code: Select all
w->model()->treeModel()->updateFeature(document)
, but the program still crashes on this line. Actually I write so:
Code: Select all
//create and fill document
...
//add document
void myMarbleWidget::addDocument()
{
    if (document != NULL)
    {
        if (this->model()->treeModel()->index(document).isValid())       //if this document has already existed
        {
           this->model()->treeModel()->updateFeature(document);  //here program crashes
        }
        else          //if adding document for the first time - it works good
        {
            this->model()->treeModel()->addDocument(document);
        }
        this->update();
    }
}
strag
Registered Member
Posts
11
Karma
0
OS

Re: How can treemodel be updated?

Wed Mar 02, 2016 10:58 am
By now the problem is solved as follows:
When I need to update the document (to add or to remove technical objects), document is removed from the treemodel, modified and added to the treemodel again.

Code: Select all
1 //when I need to add objects on the map
2    if (w->getCountRef() > 0)          //if the model contains the document
3         w->model()->treeModel()->removeDocument(w->document());

5    fillDoc();                                  //here placeMarks are added to the w->document()
6    w->addDocument();              //here document is added to the treemodel
7
8  //when I need to remove some objects from the map       
9  w->clearDocument();       //after this we have to check whether  the required objects are removed  from the map,
                         //and if necessary, to perform lines 5-6 to return required objects on the map
10
11 //used functions
12 void myMarbleWidget::addDocument()
13 {
14      if (!this->model()->treeModel()->index(doc).isValid())
15           this->model()->treeModel()->addDocument(doc);
16
17      ++countRef;
18 }
19
20 void myMarbleWidget::clearDocument()
21 {
22      if (this->model()->treeModel()->index(doc).isValid())
23            this->model()->treeModel()->removeDocument(doc);
24
25      doc->clear();
26
27     --countRef;
28  }
29
30 int myMarbleWidget::getCountRef()
31 {    return countRef;    }                  // int countRef is a private member of subclass myMarbleWidget.
                                                 //It shows how many times I tried to add document to the treemodel
32
33 void fillDoc()
34 {
35      foreach (QString st, mapSt.keys())                 
36      {
37            GeoDataPlacemark *place = new GeoDataPlacemark(st);
38            place->setCoordinate(l, b, 0, GeoDataCoordinates::Radian);
39            w->document()->append(place);
40       }
41 }


Bookmarks



Who is online

Registered users: bancha, Bing [Bot], Evergrowing, Google [Bot], mesutakcan, Sogou [Bot]