PDA

View Full Version : Obtain index of child's parent



webquinty
10th May 2009, 10:14
Hello,

I can obtain index of child and text of parent, but I am interesting to obtain index of both.


QTreeWidgetItem *parent = myItem->parent();
QString nameItem;
int index;

index = parent->indexOfChild(ui->deviceTree->currentItem());
nameItem = parent->text(indice);

any advice???
Best regards

caduel
10th May 2009, 10:19
well, to get the index of the parent, you must ask the grandparent, i.e. the parent of the parent, (or the QTreeWidget, if no grandparent exists)

webquinty
11th May 2009, 10:05
well, to get the index of the parent, you must ask the grandparent, i.e. the parent of the parent, (or the QTreeWidget, if no grandparent exists)

Any example to understand it???

thank you very much.

Lykurg
11th May 2009, 11:16
QTreeWidgetItem *child = /*currentItem() or whatever*/;
QTreeWidgetItem *parent = child->parent(); //check if valid
QTreeWidgetItem *grandparent = parent->parent(); //check if valid
int index_child = parent->indexOfChild(child);
int index_parent = grandparent->indexOfChild(parent);

webquinty
11th May 2009, 12:57
QTreeWidgetItem *child = /*currentItem() or whatever*/;
QTreeWidgetItem *parent = child->parent(); //check if valid
QTreeWidgetItem *grandparent = parent->parent(); //check if valid
int index_child = parent->indexOfChild(child);
int index_parent = grandparent->indexOfChild(parent);


Thank you very much, I did not understand what was grandparent.

Best regards