PDA

View Full Version : QTreeWidgetItem relatiionship



chaoticbob
26th October 2008, 05:18
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.

JimDaniel
26th October 2008, 05:52
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?

chaoticbob
26th October 2008, 07:03
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:


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 false


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

Thanks.

JimDaniel
26th October 2008, 15:23
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.

wysota
26th October 2008, 15:42
bool isParentOf(QTreeWidgetItem *item1, QTreeWidgetItem *item2){
if(item2==item1 || item1==0 || item2==0) return false;
while(item2!=0){
if(item2==item1) return true;
item2 = item2->parent();
}
return false;
}

chaoticbob
26th October 2008, 19:58
Thanks guys.

What wysota suggested works well enough.