qt - Assigning Icons to items in QTreeView -
i have class inheriting qtreeview. need assign icons different types of files , directories. there solution given this question:
qvariant myqfilesystemmodel::data( const qmodelindex& index, int role ) const { if( role == qt::decorationrole ) { qfileinfo info = myqfilesystemmodel::fileinfo(index); if(info.isfile()) { if(info.suffix() == "dat") return qpixmap(":/icons/file_icon.png");//i pick icon depending on extension else if(info.suffix() == "mcr") return qpixmap(":/icons/region_icon.png"); } } return qfilesystemmodel::data(index, role); }
my class not inherit qfilesystemmodel rather compose in, means cannot overide function data()
. above, how icons show up? calling data() in constructor?
you need add model tree root node:
qstandarditemmodel* model = new qstandarditemmodel; qstandarditem * rootnode = model->invisiblerootitem(); this->setmodel(model); //your class inherts qtreeview
and add item icon :
qstandarditem* item = new qstandarditem("myitem") item->seticon(qicon("icon.jpg")); rootnode->appendrow(item);
you may have : http://hpics.li/e9dc5dd
Comments
Post a Comment