Results 1 to 3 of 3

Thread: How to get parent of selected item in TreeView?

  1. #1
    Join Date
    Feb 2009
    Location
    Russia, Siberia, Novosibirsk
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to get parent of selected item in TreeView?

    Hello, guys. At first, sorry for my bad english...

    For my purposes I was created widget kinda "date picker", based on model/view architecture. Widget haves the next structure (for example):
    Qt Code:
    1. 2000
    2. January
    3. 1
    4. 2
    5. 3
    6. ...
    7. February
    8. 1
    9. 2
    10. 3
    11. ...
    12. Other months...
    13. 2001
    14. January
    15. 1
    16. ...
    17. 2002
    18. 2003
    19. ...
    20. etc
    To copy to clipboard, switch view to plain text mode 

    User can pick a date. Not just a day, he can also pick a month, or a year.
    It's so easy to get data of the selected item, just
    Qt Code:
    1. value = model->data(treeWidget->currentIndex(), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    but how to get the parent of the selected item? In case when user selected a month or a day, we need to know, for which year belongs this month (or for which year and month belongs this day).
    So, I need help How to get data of the parent of the selected item?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get parent of selected item in TreeView?

    Qt Code:
    1. QTreeWidgetItem *item = treeWidget->currentItem();
    2. value = item->data();
    3. QTreeWidgetItem *item_parent = item->parent();
    4. if (item_parent) ....
    To copy to clipboard, switch view to plain text mode 

    I suggest not to use the "DisplayRole" here.
    I would add a custom role for each item that contains the QDate so you don't have to build up dates from displayed data. Maybe another role that tells you if it is a year/month/day item.
    (You just have to figure out how you represent months and so on (a QDate can't have a day value of 0.)

    HTH

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

    RavenS (7th March 2009)

  4. #3
    Join Date
    Feb 2009
    Location
    Russia, Siberia, Novosibirsk
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get parent of selected item in TreeView?

    Huh, thank you very much! I just didn't saw this method.

    And also thanks for suggestion, I'll think about it.

Similar Threads

  1. Replies: 4
    Last Post: 25th September 2008, 13:54
  2. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  3. current item in treeview
    By kernel_panic in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2007, 19:26
  4. Changing selected item color in non-current window.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2007, 07:09
  5. QTreeView: selection behavior upon selected item removal
    By Pieter from Belgium in forum Qt Programming
    Replies: 6
    Last Post: 11th July 2007, 16:00

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.