Results 1 to 6 of 6

Thread: How to check/uncheck all the items in a QTreeWidget?

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question How to check/uncheck all the items in a QTreeWidget?

    Hi,

    I'm using QT Creator to develop an small application. I have a QTreeWidget in the UI and I use the following code to populated:

    Qt Code:
    1. QTreeWidgetItem *mainhh = new QTreeWidgetItem(this->ui->treetables);
    2. mainhh->setText(0, tr("Main Table"));
    3. QStringList lista;
    4. //Move the items to the list
    5. lista << tr("Sub-table 1");
    6. lista << tr("Sub-table 2");
    7. lista << tr("Sub-table 3");
    8. //Many more items ........
    9. //Add the list of subtables as a children of Main Table
    10. new QTreeWidgetItem(mainhh,lista,0);
    11. //Many more parent nodes and childs.......
    12. this->ui->treetables->expandAll(); //Expand the tree
    To copy to clipboard, switch view to plain text mode 

    The code works fine but how can I check/uncheck all the items after I have fulled populated the tree e.g., by clicking a button called "Check/Uncheck all". I guess I need to set some flags and properties to each items with something like:
    Qt Code:
    1. setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    2. setCheckState(Qt::Unchecked); //or QT::Checked
    To copy to clipboard, switch view to plain text mode 

    But.... I don't know how to go through all the items in the tree to set those properties and flags to each item.

    Many thanks,
    QLands
    Last edited by qlands; 5th March 2010 at 09:39. Reason: Some clarifications

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to check/uncheck all the items in a QTreeWidget?

    Can't you do it while populating the tree?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check/uncheck all the items in a QTreeWidget?

    Well... Yes I can but I still need to implement a "Check/Uncheck all" button. And for this I would like to use a FOR statement. For example, if I use a QListWidget is can implement the code really easy with:
    Qt Code:
    1. for (pos =0; pos <= this->ui->lsttables->count()-1; pos++)
    2. {
    3. this->ui->lsttables->item(pos)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    4. this->ui->lsttables->item(pos)->setCheckState(Qt::Unchecked);
    5. }
    To copy to clipboard, switch view to plain text mode 

    But, with a QTreeWidget, I have no clue how to do it.

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to check/uncheck all the items in a QTreeWidget?

    Qt Code:
    1. void checkSubTree(QTreeWidgetItem *item, Qt::CheckState st){
    2. item->setCheckState(0, st);
    3. for(int i=0;i<item->childCount();i++)
    4. checkSubTree(item->child(i), st);
    5. }
    6.  
    7. for(int i=0;i<tree->topLevelItemCount();i++){
    8. checkSubTree(tree->topLevelItem(i), Qt::Checked);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Be warned it might be very slow for many items. A model-based approach should be much faster (apart when using QStandardItemModel).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check/uncheck all the items in a QTreeWidget?

    Brilliant...

    Thanks a lot.

  6. #6
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to check/uncheck all the items in a QTreeWidget?

    maybe you can use a QList<QTreeWidgetItem *> to store all the point of all the item object , and just use a for loop to check/uncheck , this will be more faster and just use a little of memory .....

Similar Threads

  1. Checkboxes: check and uncheck
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2008, 21:26
  2. Replies: 5
    Last Post: 6th March 2008, 19:04
  3. QTreeWidget (Row Check, Custom Sort)
    By AaronMK in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2008, 15:55
  4. Check and Uncheck on a Dir Tree?
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2007, 11:55
  5. QListWidgetItem check/uncheck
    By Arthur in forum Qt Programming
    Replies: 6
    Last Post: 12th May 2006, 10:19

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.