Qt: icons used in treemodel + qtreeview

Source: Internet
Author: User

 

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.

 

 

 

 

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.