Results 1 to 5 of 5

Thread: amount of items in QTreeWidget

  1. #1
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default amount of items in QTreeWidget

    As the title says, is there a way to get the amount of QTreeWidgetItems in a QTreeWidget?

    Is there also a way to get a specified item by its location in the QTreeWidget? For example:
    Qt Code:
    1. QTreeWidget* tree = new QTreeWidget();
    2. int index = tree->addTopLevelItem(item);
    3. QTreeWidgetItem* itemAtIndex = tree->getItemAtIndex(index);
    4. if(item == itemAtIndex)
    5. cout << "works!";
    6. int amount = tree->getItemCount();
    7. for(int i = 0; i < amount; i++)
    8. cout << tree->getItemAtIndex(i).text(0).toStdString();
    To copy to clipboard, switch view to plain text mode 

  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: amount of items in QTreeWidget

    You can access top level items via QTreeWidget::topLevelItem(int index) and children of particular items via QTreeWidgetItem::child(int index). But there is no way to access arbitrary items in the tree with an "universal" index. However, QTreeWidgetItemIterator might become handy in case of looking for certain items.
    J-P Nurmi

  3. #3
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: amount of items in QTreeWidget

    Thanks for the fast reply.

    I found out QWidgetItemIterator already but it was just not I was looking for... I need these functions actually for a music player to go to a random next song. Example:
    Qt Code:
    1. void MP3::playNextSong()
    2. {
    3. std::cout << "void MP3::playNextSong() called" << std::endl;
    4. timer->stop();
    5. QTreeWidgetItemIterator it(g_main->musicList);
    6. bool found = false;
    7. int random = rand()%10;
    8. while(random == id)
    9. random = rand()%10;
    10. std::cout << "void MP3::playNextSong() rand=" << random << std::endl;
    11. while(*it)
    12. {
    13. QTreeWidgetItem* item = *it;
    14. if(item && item->text(1).toInt() == random)
    15. {
    16. load(item->text(0).toStdString(), random);
    17. play();
    18. found = true;
    19. break;
    20. }
    21. ++it;
    22. }
    23. if(!found)
    24. {
    25. std::cout << "void MP3::playNextSong() found=false " << std::endl;
    26. playNextSong();
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    Now I'm using "rand()%10" only for testing with 10 items. But I need to know the actual count of the items in the tree, or I would have to manually change the count all the time...

    Of course I can also try to make my own extended class and do this:
    Qt Code:
    1. MyTreeWidget::addTopLevelItem(QTreeWidgetItem* item)
    2. {
    3. this->count++;
    4. QTreeWidget::addTopLevelItem(item);
    5. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: I just saw this function too: QTreeWidget::topLevelItemCount (). Is this the function I'm looking for?
    EDIT 2: Works now . But is there a way to hide a column of a QTreeWidget?
    Last edited by supergillis; 1st August 2008 at 22:05.

  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: amount of items in QTreeWidget

    Quote Originally Posted by supergillis View Post
    EDIT: I just saw this function too: QTreeWidget::topLevelItemCount (). Is this the function I'm looking for?
    Sure, if you have top level items only ie. you don't have hierarchical structure.

    EDIT 2: Works now . But is there a way to hide a column of a QTreeWidget?
    See QTreeView::setColumnHidden().
    J-P Nurmi

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

    supergillis (3rd August 2008)

  6. #5
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: amount of items in QTreeWidget

    Great man! Thanks!

Similar Threads

  1. Drag and Drop in a QTreeWidget subclass
    By kishore in forum Qt Programming
    Replies: 2
    Last Post: 14th May 2008, 07:12
  2. QTreeWidget - locating items in viewport
    By kemp in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2007, 15:18
  3. Having trouble clearing a QTreeWidget.
    By Nyphel in forum Qt Programming
    Replies: 28
    Last Post: 10th October 2007, 15:33
  4. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 12:20
  5. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 12:20

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.