c++ - Qt QTreeView not updating when adding to model -


the source code relating question available on public git repository on bitbucket.

i'm trying dynamically add items qtreeview model using following code in mainwindow.cpp:

if(dlg->exec() == qdialog::accepted) {     qlist<qvariant> qlist;     qlist << item.name << "1111 0000" << "0x00";     hiddescriptortreeitem *item1 = new hiddescriptortreeitem(qlist, hiddescriptortreemodel->root());     hiddescriptortreemodel->root()->appendchild(item1); } 

this works when run within mainwindow constructor, after ui->setupui(this), need run within event filter, same code doesn't qtreeview updating. when set breakpoint @ mainwindow.cpp:70 , step through next few lines, can see data being added model, need qtreeview refresh.

i understand done emitting datachanged(), not sure how this. signal signature datachanged signal looks follows:

void datachanged(const qmodelindex &topleft, const qmodelindex &bottomright, const qvector<int> &roles = qvector<int>()); 

so need come topleft , bottomright qmodelindex instances. how build/obtain these item1 in above snippet?

also, begininsertrows() , endinsertrows() come view this, should calling these functions?

from qabstractitemmodel documentation:

void qabstractitemmodel::begininsertrows ( const qmodelindex & parent, int first, int last ) [protected] begins row insertion operation. when reimplementing insertrows() in subclass, must call function before inserting data model's underlying data store. parent index corresponds parent new rows inserted; first , last row numbers new rows have after have been inserted. 

the other protected functions similar things.

and insertrows() says:

if implement own model, can reimplement function if want support insertions. alternatively, can provide own api altering data. in either case, need call begininsertrows() , endinsertrows() notify other components model has changed.

take qabstractitemmodel protected functions , signals

views connect signals know when model data changes , rearrange data inside. functions emit signals internally make easy warn view when has happenned. signals can emitted abstract class.

components connected signal use adapt changes in model's dimensions. can emitted qabstractitemmodel implementation, , cannot explicitly emitted in subclass code.

so have stick methods.

edit in answer comment:

indeed, items should have reference model , tell changes, check theses lines qstandarditem:

void qstandarditem::emitdatachanged()

void qstandarditem::removerows(int row, int count)

( note, how, in second, calls model's rowsabouttoberemoved() , rowsremoved() )

maybe should try use qstandarditem , qstandarditemmodel. either direct or subclassing. hide lot of ugly stuff.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -