Results 1 to 6 of 6

Thread: QTreeWidget

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default QTreeWidget

    Hello forum,

    There are functions to add the top level item in the QTreeWidget. How to insert the child level items? Please check the following snippet:


    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc,argv);
    4.  
    5. QTreeWidget *treeWidget = new QTreeWidget();
    6.  
    7. QStringList itemList;
    8. itemList << "World";
    9.  
    10. treeWidget->insertTopLevelItem(0,new QTreeWidgetItem(itemList));
    11.  
    12. treeWidget->setColumnCount(1);
    13. QList<QTreeWidgetItem *> items;
    14. for (int i = 0; i < 10; ++i)
    15. items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
    16. treeWidget->insertTopLevelItems(1, items);
    17.  
    18. treeWidget->show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 


    I want all the items added after the "World" will be under the hierarchy view of the "World" so that the child items can be collasped or expanded. But all i am getting them as list.

    Basically i would like to have the "World" as the root item and rest of the items as its children.



    Thanks
    Sajjad

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget

    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    sajis997 (19th April 2012)

  4. #3
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget

    Hi forum,

    I have more issues over qtreewidget. In one of the functions in my project i am pulling out the invisible root item with the following function

    Qt Code:
    1. QTreeWidgetItem *rootItem = treewidget->invisibleRootItem()
    To copy to clipboard, switch view to plain text mode 

    Then i send this item to other function to add widget item recursively in several functions. Apparantly, it may seem a complicated way to populate a tree, but i need to do as i am doing some other processing in those functions.

    Now i created a new tree widget item with the parent item(root item in the first pass) as the parent as follows :

    Qt Code:
    1. //create a new tree item with the tree_item as the parent
    2. QTreeWidgetItem *new_widget_item = new QTreeWidgetItem(tree_item);
    3. //set the text for the new tree widget item
    4. new_widget_item->setText(0,QString(tree_string.c_str()));
    To copy to clipboard, switch view to plain text mode 

    I want to add the new item into the qtreewidgt and i am not sure which function to use. qtreewidget has several item like; insertTopLevelItem(...), but in that case you have to know the index value.

    The issue showed up while porting an wxwidget application and they are adding the item as follows:

    Qt Code:
    1. wxTreeItemId new_id = TreeViewTree->AppendItem( tree_id, wxString( tree_string.c_str(), wxConvUTF8 ) );
    To copy to clipboard, switch view to plain text mode 

    On the above snippet the treeviewtree is equivalent to the qtreewidget and tree_id is equivalent to the new_widget_item in Qt.


    I am looking for some equivalent function in Qt.


    Need some hint here.



    Regards
    Sajjad

  5. #4
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget

    Hi forum,

    No reply so far, i believe i am still ambiguous. Let me try again. I have pulled out the invisible root item from the tree widget as follows:

    Qt Code:
    1. QTreeWidgetItem *rootItem = treewidget->invisibleRootItem();
    To copy to clipboard, switch view to plain text mode 

    Then i initialize another tree widget item root item as the parent as follows:

    Qt Code:
    1. //create a new tree item with the tree_item as the parent
    2. QTreeWidgetItem *new_widget_item = new QTreeWidgetItem(rootItem);
    3. //set the text for the new tree widget item
    4. new_widget_item->setText(0,QString(tree_string.c_str()));
    To copy to clipboard, switch view to plain text mode 

    And i want the new tree widget item to the tree widget. Could you suggest the function inside the qtreewidget class ?


    Please let me know if i am still not clear enough

    Regards
    Sajjad

  6. #5
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget

    Hello folks,


    I am eagerly waiting for some hint to get around with this issue.


    Regards
    Sajjad

  7. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget

    Did you try the code you've posted?
    As it should be all you need to do, to see your items in the tree.

    When you parent a child to the root node you don't need to add it to the tree any more.
    every child is added to the tree in the order they're parented.

    Consider following example:
    Qt Code:
    1. // mainwindow.cpp constructor
    2. QTreeWidget* tree = new QTreeWidget( this );
    3. this->setCentralWidget( tree );
    4.  
    5. QTreeWidgetItem* root = tree->invisibleRootItem();
    6. QTreeWidgetItem* node = NULL;
    7. QTreeWidgetItem* leaf = NULL;
    8.  
    9. node = new QTreeWidgetItem( root );
    10. node->setText( 0, "node 1" );
    11. leaf = new QTreeWidgetItem( node );
    12. leaf->setText( 0, "leaf 1" );
    13.  
    14. node = new QTreeWidgetItem( root );
    15. node->setText( 0, "node 2" );
    16. leaf = new QTreeWidgetItem( node );
    17. leaf->setText( 0, "leaf 2" );
    To copy to clipboard, switch view to plain text mode 

    I hope this solves your problem.

Similar Threads

  1. QTreeWidget
    By Kazuma in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2011, 09:58
  2. Replies: 2
    Last Post: 17th March 2008, 12:53
  3. look through a QTreeWidget
    By mattia in forum Newbie
    Replies: 5
    Last Post: 20th December 2007, 15:07
  4. Replies: 1
    Last Post: 19th October 2007, 01:29
  5. QTreeWidget
    By allensr in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2006, 16:24

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
  •  
Qt is a trademark of The Qt Company.