QT Model/viel/delegate model/view/delegate, the function is very powerful, but to be proficient in the application need to do more hands, can master.
At present, the degree of understanding is very small, first write it down ...
Define the model to inherit from Qabstractitemmodel, define the item used in the model, can be customized, can also inherit, not yet use the delegate delegate
Show view by View->setmodel ();
Qtreeview
Model=new TreeModel ();
view = new Qtreeview (this); View->setmodel (model);
View->setcolumnwidth (0,150);
View->resizecolumntocontents (1);//Column Width adaptive adjustment
View->setcolumnhidden (2,true);//Hide second column
View->setstylesheet ("qtreeview::item{height:25px;show-decoration-selected:0;}"
"Qtreeview::item:selected{background-image:url (image/list_select_bg.png);}"
);
TreeModel model=new Treemodel;//qabstractitemmodel
When inserting and deleting rows, the method is as follows:
BOOL Treemodel::insertrows (int position, int rows, const qmodelindex &index)
{
Begininsertrows (index, position, position+rows-1);
。。。
Add insert Operation code
Endinsertrows ();
return true;
}
BOOL Treemodel::removerows (int position, int rows, const qmodelindex &index)
{
Qdebug () << "in Removerows";
Beginremoverows (index, position, position+rows-1);
Add Delete operation code
Endremoverows ();
return true;
}
Note: If you want to regenerate the model, you need to update the model before the Model->reset (), I just read the file to generate the tree structure, each delete and save after inserting, all to update the model, if there is no Model->reset (), There will be a segment error when deleting.
Subsequent updates ...