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:
Code:
mainhh->setText(0, tr("Main Table"));
//Move the items to the list
lista << tr("Sub-table 1");
lista << tr("Sub-table 2");
lista << tr("Sub-table 3");
//Many more items ........
//Add the list of subtables as a children of Main Table
//Many more parent nodes and childs.......
this->ui->treetables->expandAll(); //Expand the tree
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:
Code:
setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
setCheckState(Qt::Unchecked); //or QT::Checked
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
Re: How to check/uncheck all the items in a QTreeWidget?
Can't you do it while populating the tree?
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:
Code:
for (pos =0; pos <= this->ui->lsttables->count()-1; pos++)
{
this->ui->lsttables->item(pos)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
this->ui->lsttables->item(pos)->setCheckState(Qt::Unchecked);
}
But, with a QTreeWidget, I have no clue how to do it.
Thanks.
Re: How to check/uncheck all the items in a QTreeWidget?
Code:
item->setCheckState(0, st);
for(int i=0;i<item->childCount();i++)
checkSubTree(item->child(i), st);
}
for(int i=0;i<tree->topLevelItemCount();i++){
checkSubTree(tree->topLevelItem(i), Qt::Checked);
}
Be warned it might be very slow for many items. A model-based approach should be much faster (apart when using QStandardItemModel).
Re: How to check/uncheck all the items in a QTreeWidget?
Brilliant...
Thanks a lot.
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 .....