Results 1 to 7 of 7

Thread: expand a treeWidget

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default expand a treeWidget

    Hello, i have a problem with treeWidget.
    I'd like to create a treeWidget and i want that all the treeWidget nodes areexpanded.
    I have tried with this code, but it doesn.'t work.
    Qt Code:
    1. bool main::setupTreeView ( QTreeWidget * thetwmTreeWidget )
    2. {
    3. QList<QTreeWidgetItem *> items;
    4.  
    5. if ( listtwmConfigPool.size() != 0 )
    6. {
    7. for ( int i = 0; i < myObject.size() ; i++ )
    8. {
    9. QTreeWidgetItem * tempQTreeWidgetItem = new QTreeWidgetItem ();
    10. tempQTreeWidgetItem->setText ( 0 , myObject.getName() );
    11. tempQTreeWidgetItem->setExpanded( true );
    12. qDebug() << tempQTreeWidgetItem->isExpanded(); // it's false...why?
    13. items.append ( tempQTreeWidgetItem );
    14.  
    15. for ( int i = 0; i < myObjectParent.size(); i++ )
    16. {
    17. QTreeWidgetItem * tempQTreeWidgetItemParent = new QTreeWidgetItem ( tempQTreeWidgetItem );
    18. tempQTreeWidgetItemParent->setText ( 0 , myObjectParent.getName() );
    19. items.append ( tempQTreeWidgetItemParent );
    20. }
    21. }
    22.  
    23. thetwmTreeWidget->setColumnCount (1);
    24. thetwmTreeWidget->insertTopLevelItems (0, items);
    25. }
    26. return TRUE;
    27. }
    To copy to clipboard, switch view to plain text mode 

    thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: expand a treeWidget

    To me it looks like you're expanding items by the time they're added nowhere yet. Have you noticed QTreeView::expandAll()?

    Edit: Oh, and I just noticed something else that's faulty too. You're adding parents and children to the same list for which you do insertTopLevelItems().
    Last edited by jpn; 6th December 2007 at 08:55.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expand a treeWidget

    yes, when i call setExpanded the items aren't allocated into the parent yet...where should i call setExpand?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: expand a treeWidget

    The code of yours is confusing because an item you call "parent" is actually created as child. However, this should do the trick:
    Qt Code:
    1. bool main::setupTreeView ( QTreeWidget * thetwmTreeWidget )
    2. {
    3. QList<QTreeWidgetItem *> items;
    4.  
    5. if ( listtwmConfigPool.size() != 0 )
    6. {
    7. for ( int i = 0; i < myObject.size() ; i++ )
    8. {
    9. QTreeWidgetItem * tempQTreeWidgetItem = new QTreeWidgetItem ();
    10. tempQTreeWidgetItem->setText ( 0 , myObject.getName() );
    11. //tempQTreeWidgetItem->setExpanded( true );
    12. //qDebug() << tempQTreeWidgetItem->isExpanded(); // it's false...why?
    13. items.append ( tempQTreeWidgetItem );
    14.  
    15. for ( int i = 0; i < myObjectParent.size(); i++ )
    16. {
    17. QTreeWidgetItem * tempQTreeWidgetItemParent = new QTreeWidgetItem ( tempQTreeWidgetItem );
    18. tempQTreeWidgetItemParent->setText ( 0 , myObjectParent.getName() );
    19. //items.append ( tempQTreeWidgetItemParent ); // <--- get rid of this
    20. }
    21. }
    22.  
    23. thetwmTreeWidget->setColumnCount (1);
    24. thetwmTreeWidget->insertTopLevelItems (0, items);
    25. thetwmTreeWidget->expandAll(); // <--- add this
    26. }
    27. return TRUE;
    28. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expand a treeWidget

    ok, in this way i can expand all the items in my treeWidget but if i want to expand just some of them?
    can't i set the expand propriety into the "for"? so i can control which node expand..

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: expand a treeWidget

    Yes, but then you will have to actually add the item to the tree before expanding it. Currently, you have a list of items that do not yet belong to any tree at all. You can for example pass the tree widget to QTreeWidgetItem constructor instead of collecting them to a list.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    mattia (6th December 2007)

  8. #7
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expand a treeWidget

    got it
    thx so much!

Similar Threads

  1. How to sort the treeWidget item according to size and date
    By santosh.kumar in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 10:32
  2. How to set the sizehint for treeWidget
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2007, 11:35
  3. By default TreeWidget width setting in DockWidget
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 31st May 2007, 05:55
  4. Replies: 2
    Last Post: 29th May 2007, 13:55
  5. How i will use treeWidget of GUI class in thread class
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 15:31

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.