Results 1 to 5 of 5

Thread: QTreeView different item delegate for child items possible?

  1. #1
    Join Date
    Jan 2006
    Posts
    18
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question QTreeView different item delegate for child items possible?

    Hi,

    How can I use a different item delegate for child items in my QTreeView?

    ParentItemA
    -ChildItemA
    -ChildItemB
    ParentItemB
    -ChildItemC

    I have found the function setItemDelegateForRow, but I don't know how to apply that just for the child items in my QTreeView. Can anyone give me a clue?

    I have been building upon the Simple Tree Model example from the Qt sources.

    Thanks,
    Royce

  2. #2
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView different item delegate for child items possible?

    I'm kind of new to Qt programming... but I have been able to put different delegates on Tree item children. I made a single ItemDelegate subclassed from QStyledItemDelegate. I had to put a switch statement for each node/child type for each of the functions: createEditor, setEditorData, setModelData, Painter and maybe setEditorGeometry to indicate which editor/editor function/painting belongs with each node type. If you can use a default display/editor for a particular node's data (like plain text), you can just have a case for calling the functions from the parent (QStyledItemEditor::Paint for example). I am storing a user class in my model, so I have to paint everything (because the defaults don't have any idea what to do with the class data!)... if you have 'normal' data, you probably don't have to define the paint function. Actually, the star rating delegate example is a very good guide. You just have to look at the itemdelegate as a single class that contains every editor/drawing function you need. The painting takes place first, and is completely separate from editing, with the editor activating only when the user asks to change data. <<that's what I think so far, in any case>>. Hope it helps!

  3. The following user says thank you to LynneV for this useful post:

    Royceybaby (7th January 2010)

  4. #3
    Join Date
    Jan 2006
    Posts
    18
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView different item delegate for child items possible?

    I have a subclassed QItemDelegate that does some formatting for my columns already. So adding a switch in to draw the child and parent rows differently is not a problem.

    The only thing I can't get my head round is how will the QItemDelegate:aint function know whether the item it is drawing is a parent item or child item and enter the correct switch path. I am sure it is simple but I just can't think how.

    Royce

  5. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView different item delegate for child items possible?

    Quote Originally Posted by Royceybaby View Post
    I have a subclassed QItemDelegate that does some formatting for my columns already. So adding a switch in to draw the child and parent rows differently is not a problem.

    The only thing I can't get my head round is how will the QItemDelegate:aint function know whether the item it is drawing is a parent item or child item and enter the correct switch path. I am sure it is simple but I just can't think how.

    Royce
    if you have only two levels then parent items have invalid parent (index.parent().isValid() gives false for top level items) and thier children have valid parents.

    If your structure is more complicated then you can use Qt::UserRole and above to store your data:
    With QStandardItemModel it would look like this:
    Qt Code:
    1. // when creating an item you add:
    2. item->setData(1, Qt::UserRole); // parent item
    3. // or
    4. item->setData(2, Qt::UserRole); // child
    5.  
    6. // then in delegate paint()
    7. if (index.data(Qt::UserRole).toInt() == 1) {
    8. // parent item
    9. }
    10. if (index.data(Qt::UserRole).toInt() == 2) {
    11. // child item
    12. }
    To copy to clipboard, switch view to plain text mode 
    And if you have you own model class then return proper value (in my example it is 1 or 2) for Qt::UserRole in your model data() method.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. The following 2 users say thank you to faldzip for this useful post:

    hongjq (22nd March 2010), Royceybaby (7th January 2010)

  7. #5
    Join Date
    Jan 2006
    Posts
    18
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView different item delegate for child items possible?

    Thank you both for your help. Been banging my head on my desk trying to work that out and now I understand a little bit more and have got it working!

Similar Threads

  1. Replies: 5
    Last Post: 10th August 2009, 10:50
  2. QTreeView Delegate
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2009, 09:10
  3. Use delegate to draw different type of items
    By nifei in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2009, 13:16
  4. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55
  5. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37

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.