Results 1 to 3 of 3

Thread: Adding Icons to QTreeView-QAbstractItemModel

  1. #1
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Adding Icons to QTreeView-QAbstractItemModel

    Hi,

    I am using QTreeView model derived from QAbstractItemModel. I am following "SimpleTreeModelExample" provided by Qt. I have two columns with parent/child hierarchy. I want to add Icon (one for each Parent, one for each child).. But i don't know how to add Icon as part of TreeItem and how to modify the Model ::data function with respect to decoration role. Please help me with some example code

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding Icons to QTreeView-QAbstractItemModel

    First, your data() method has to know whether the icon is being requested for a child or a parent. You can test that by determining whether QModelIndex::parent() returns a valid QModelIndex. If it does it's a child node, if not, it's a parent node.

    Then, you check to see if the role being passed in the data() method is Qt::DecorationRole. If it is, then you check to see if QModelIndex::column() is zero. If it is, then you return a QVariant containing the QIcon instance for either the parent or the child node, whichever you determined in the first step.

    If every parent has the same icon, and every child has the same icon (but different from the parent), you can simply store two QIcon instances as member variables in your abstract item model class, load them from resources when the model is created, and simply return a QVariant contain the right QIcon. If you have a lot of different icons, then you may need to store the correct one with every item in your model.

    There is a simple example in the second answer here.

    Note that you do not use QTreeWidgetItem if you have constructed your own QAbstractItemModel class and are using QTreeView to display it. QTreeWidgetItem is a special class used only with QTreeWidget, and represents an item in the internal custom model used by the widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Adding Icons to QTreeView-QAbstractItemModel

    Thank you very much for the detailed explanation

Similar Threads

  1. QTableView + QAbstractItemModel and adding rows
    By karlkar in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2016, 22:45
  2. Replies: 2
    Last Post: 8th April 2013, 07:40
  3. Adding Icons
    By bijan311 in forum Newbie
    Replies: 2
    Last Post: 27th March 2010, 23:43
  4. QTreeView with QAbstractItemModel
    By Fletcher in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2009, 00:02
  5. Need help with QTreeView/QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th August 2006, 16:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.