Results 1 to 3 of 3

Thread: QTreeWidget set invisible root node?

  1. #1
    Join Date
    Sep 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTreeWidget set invisible root node?

    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.

  2. #2
    Join Date
    Feb 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget set invisible root node?

    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:
    Qt Code:
    1. class A{
    2. private:
    3. Item _root;
    4. public:
    5. Item root() const {return _root;}
    6. };
    7.  
    8. class Item : public QTreeWidgetItem{
    9.  
    10. };
    11.  
    12. class Widget : public QTreeWidget{
    13.  
    14.  
    15. public:
    16. void showItems(A* a){
    17. Item* top = a->root();
    18. for (int i = 0; i < top->childCount() ; i++){
    19. addTopLevelItem(top->child(i));
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    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!

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTreeWidget set invisible root node?

    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:
    Qt Code:
    1. void showItems(A* a){
    2. Item* top = a->root();
    3. while (top->childCount() > 0){
    4. Item *child = top->takeChild(0);
    5. addTopLevelItem(child);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Copy:
    Qt Code:
    1. void showItems(A* a){
    2. Item* top = a->root();
    3. for(int i=0;i<top->childCount();++i) {
    4. Item *child = new Item(*top->child(i));
    5. addTopLevelItem(child);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTreeWidget: How to display the root item?
    By Johannes in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2011, 11:39
  2. customize the root node of a treeview
    By billconan in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 09:06
  3. Inserting node to Xml file
    By vajindarladdad in forum Newbie
    Replies: 2
    Last Post: 13th April 2009, 12:34
  4. how to justfy a node is leaf node or not
    By weixj2003ld in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2009, 08:40
  5. remove node in xml file
    By mattia in forum Newbie
    Replies: 1
    Last Post: 6th March 2008, 14:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.