Here's some code I use to expand the tree all the way to the root.

It's a recursive function that accepts a QTreeWidgetItem *item pointer and travels up the tree, starting as position of *item, and expands all parents in the *item's branch until it reaches the *item's topLevelItem.

Qt Code:
  1. void MyWidget::expandTree(QTreeWidgetItem *item) {
  2. QTreeWidgetItem *parent = item->parent();
  3. if (parent) {
  4. parent->setExpanded(true);
  5. int topLevel = table->indexOfTopLevelItem(parent);
  6. if (topLevel!=-1) {
  7. QTreeWidgetItem *root = table->topLevelItem(topLevel);
  8. root->setExpanded(true);
  9. } else {
  10. if (item->parent()) {
  11. expandTree(item->parent());
  12. }
  13. }
  14. }
  15. }
To copy to clipboard, switch view to plain text mode