Qtreeview/qabstractitemmodel Usage Summary

Source: Internet
Author: User
Tags bool reset

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 ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.