PDA

View Full Version : QTreeWidget set invisible root node?



Braf
19th September 2011, 07:01
I have a QTreeWidget whose contents is changed based on which document is currently active. Each document stores the items as a QTreeWidgetItem representing the root of the tree so that I can easily pass it to recursive functions for processing the tree, but this item is really supposed to play the role of the "invisible root node" in the QTreeWidget. That is, the children of that node are meant to appear as the top level items.

What is the best way to achieve this? If I simply add the children of the root as top level items of the QTreeWidget, they don't appear at all, and there is no corresponding setter for the invisibleRootNode property.

excalibur1491
9th February 2013, 19:35
I have the exact same problem! Does any one know any workaround for this?

I have a class "Item" that inherits from QTreeWidgetItem, a class "A" that contains an "Item" called _root and a class inheritant from QtreeWidget:


class A{
private:
Item _root;
public:
Item root() const {return _root;}
};

class Item : public QTreeWidgetItem{

};

class Widget : public QTreeWidget{


public:
void showItems(A* a){
Item* top = a->root();
for (int i = 0; i < top->childCount() ; i++){
addTopLevelItem(top->child(i));
}
}
}


This does not display anything on the Widget! I don't undesrtand why because if I simple do: addTopLevelItem(top); instead of the loop it works (but it is not what I want). One way would be to be able to set the invisibleRootItem, but there is no setter....


Any idea? Thank you all!

norobro
10th February 2013, 03:03
Not sure that I understand what you are trying to achieve, but I think to get anything to show up in your treeWidget you to need to either reparent the items or copy them.

Reparent:
void showItems(A* a){
Item* top = a->root();
while (top->childCount() > 0){
Item *child = top->takeChild(0);
addTopLevelItem(child);
}
}
Copy:
void showItems(A* a){
Item* top = a->root();
for(int i=0;i<top->childCount();++i) {
Item *child = new Item(*top->child(i));
addTopLevelItem(child);
}
}