[Solved] Qt - How to get parent of QTreeWidgetItem
-
Hi,
Does anyone know how to get parent of a QTreeWidgetItem child?
Thanks!!
-
What's wrong with the existing QTreeWidgetItem::parent() method?
-
Yes...I suppose I worded my question incorrectly, but thank you.
I meant how can I return the string of the parent in C++ as that method returns a QTreeWidgetItem.
-
treeWidgetItem->parent()->text() ?
-
:) Thanks, DerManu again!
I tried:
treeWidgetItem->parent()->text(column)
Works!!
-
When dereferencing a pointer, it is wise to check that the pointer is valid first. In your case, the result of parent() may be 0, and then the ->text(column) call would cause a crash.
-
Thank you, Andre. I also noticed that it would crash if the item was the uppermost item & had no parent.
I added this check to my c++ code (might be useful for anyone who comes across this in the future):
@ QString cur = item->text(column); int root = ui->treeWidget->indexOfTopLevelItem(item); if ( root.parent() != None ) { // not top most QString parent = item->parent()->text(column); }@
-
Are you sure this case isn't covered by what Andre said? I would expect parent() to simply return 0 on top level items (but I haven't checked that).