First, you must build a model by yourself. In fact, it is to implement the abstract class q1_actitemmodel. There are many articles about this process, such
Bytes. The simpletreemodel project path is usually in the itemviews directory of the example directory in the qt directory. For example, my path is in C:/Qt/2010.05/qt/examples/itemviews. The example in this article is based on this demo to implement the function of displaying icons on the treemodel node.
1. First, find a map file. Here test.png is used as an example, and put it in the program code directory. Then, add the image to simpletreemodel. prc in the project column of qtcreator.
2. In the header file treemodel. h, add an icon variable.
QIcon * m_icon;
3. In treemodel. cpp
TreeModel: TreeModel (const QString & data, QObject * parent) <br/>: q1_actitemmodel (parent) <br/>{< br/> QList <QVariant> rootData; <br/> rootData <"Title" <"Summary"; <br/> rootItem = new TreeItem (rootData); <br/> setupModelData (data. split (QString ("/n"), rootItem); <br/> m_icon = new QIcon (":/test.png"); // initialization icon <br/>}
This line adds the initialization icon.
4. In the data () function, modify it:
Qvariant treemodel: Data (const qmodelindex & Index, int role) const <br/>{< br/> If (! Index. isvalid () <br/> return qvariant (); <br/> If (role = QT: decorationrole & <br/> index. column () = 0 & // The first column of nodes <br/> rowcount (INDEX) = 0 // The number of subnodes is 0 <br/>) <br/>{< br/> return * m_icon; <br/>}< br/> If (role! = QT: displayrole) <br/> return qvariant (); <br/> treeitem * Item = static_cast <treeitem *> (index. internalpointer (); <br/> return item-> data (index. column (); <br/>}
You can,
Reference http://blog.csdn.net/firefly_liu/archive/2009/03/27/4030589.aspx
But now there is a problem, that is, the program is running correctly, and the icon is displayed, but every time you close the program, a segment error will be reported.