Results 1 to 6 of 6

Thread: QTreeWidgetItem relatiionship

  1. #1
    Join Date
    Oct 2008
    Posts
    37
    Thanked 2 Times in 2 Posts

    Default QTreeWidgetItem relatiionship

    Hi,

    I was wondering if there is a way to determine if two QTreewidgetItems are related? I looked through the docs - but I couldn't find a member function of either QTreeWidget or QTreeWidgetItem that could give me what I want.

    Thanks.

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTreeWidgetItem relatiionship

    It depends on what you mean by related. You could call parent() on the two items, if the returned parent pointer is the same, the two items are siblings in the tree, is this what you mean?

  3. #3
    Join Date
    Oct 2008
    Posts
    37
    Thanked 2 Times in 2 Posts

    Default Re: QTreeWidgetItem relatiionship

    Sorry - I should have stated a bit more detail. Basically if given two items (ItemA, ItemD) - I want to be able to determine if ItemD is a child of ItemA.

    For instance:
    Qt Code:
    1. ItemA
    2. |--ItemB
    3. |--ItemC
    4. |--ItemD
    5. ItemE
    6. ItemF
    7.  
    8. ItemD->isChildOf( ItemA ) - this would return true
    9. ItemD->isChildOf( ItemB ) - this would return true
    10. ItemD->isChildOf( ItemC ) - this would return true
    11. ItemD->isChildOf( ItemE ) - this would return false
    12. ItemA->isChildOf( ItemB ) - this would return false
    To copy to clipboard, switch view to plain text mode 

    ....a isParentOf relationship work as well...

    Thanks.

  4. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTreeWidgetItem relatiionship

    Hmm, looks like you might need to keep track of that yourself somehow. Maybe just pass down a list of parent pointers to each new node. Then when you call isChildOf(), it can just query the list for a match? Doesn't seem too elegant, but it might get the job done.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidgetItem relatiionship

    Qt Code:
    1. bool isParentOf(QTreeWidgetItem *item1, QTreeWidgetItem *item2){
    2. if(item2==item1 || item1==0 || item2==0) return false;
    3. while(item2!=0){
    4. if(item2==item1) return true;
    5. item2 = item2->parent();
    6. }
    7. return false;
    8. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to wysota for this useful post:

    JimDaniel (26th October 2008)

  7. #6
    Join Date
    Oct 2008
    Posts
    37
    Thanked 2 Times in 2 Posts

    Default Re: QTreeWidgetItem relatiionship

    Thanks guys.

    What wysota suggested works well enough.

Similar Threads

  1. Custom QTreeWidgetItem context menu?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 1st February 2010, 04:42
  2. Replies: 3
    Last Post: 26th April 2008, 18:42
  3. select a QTreeWidgetItem
    By mattia in forum Newbie
    Replies: 2
    Last Post: 21st December 2007, 11:01
  4. QTreeWidgetItem ?
    By allensr in forum Qt Programming
    Replies: 5
    Last Post: 3rd January 2007, 17:51
  5. QTreeWidgetItem swap or move up one level problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2006, 18:34

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
  •  
Qt is a trademark of The Qt Company.