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.
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.
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?
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:
ItemA |--ItemB |--ItemC |--ItemD ItemE ItemF ItemD->isChildOf( ItemA ) - this would return true ItemD->isChildOf( ItemB ) - this would return true ItemD->isChildOf( ItemC ) - this would return true ItemD->isChildOf( ItemE ) - this would return false ItemA->isChildOf( ItemB ) - this would return falseTo copy to clipboard, switch view to plain text mode
....a isParentOf relationship work as well...
Thanks.
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.
Qt Code:
if(item2==item1 || item1==0 || item2==0) return false; while(item2!=0){ if(item2==item1) return true; item2 = item2->parent(); } return false; }To copy to clipboard, switch view to plain text mode
JimDaniel (26th October 2008)
Thanks guys.
What wysota suggested works well enough.
Bookmarks