c++ - Storing custom objects in QStandardItemModel -


i'd store custom objects (let's instances of mydataclass) in tree structure, , linked view. used qstandarditemmodel. think mydataclass should inherit qstandarditem :

class mydataclass : public qstandarditem { public:     mydataclass(qstring name) private:     vector<float> somedata; } 

but cannot figure out how store instances of class in qstandarditemmodel. tried qstandarditem.setchild , appendrow not work , think don't qstandarditemmodel thing. think solution deals woth qstandarditem.setdata cannot figure out how works custom objects.

i have make work using qvariant. fill model custom data :

mydataclass *data; ...  // adding data  qvariant variant; variant.setvalue(data);  qstandarditemmodel model; // here model  qstandarditem *parentitem = model.invisiblerootitem(); qstandarditem *item = new qstandarditem();  item->setdata(variant); parentitem->setchild(0, 0, item); // adding item root 

later, when want retrieve data :

mydataclass *retrieveddata = model.invisiblerootitem()->                                      child(0, 0)->data().value<mydataclass*>(); 

note had add line in class declaration :

class mydataclass : public qstandarditem { public:     mydataclass(qstring name) private:     vector<float> somedata; }  q_declare_metatype(mydataclass *) // add line 

thank help.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -