Solved changing the checkstate of all the child item by checking the root item in Qtree widget
-
Hello
I want to change the checkstate of the tree items like fallows- if i click on the child item it should change (this i am able to do)
- if i click on the root it should change the check state of child items too
here is the code
``` item->setExpanded(true); item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); item->setCheckState(0, Qt::Unchecked);
void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) { if ( item->checkState(column) == Qt::Checked) { qDebug() << item->text(column); } }
and after checking i want to store all the checked items to a list please someone help me out in this
Thanks in advance
-
@kook said in changing the checkstate of all the child item by checking the root item in Qtree widget:
and after checking i want to store all the checked items to a list
You asked this already here: https://forum.qt.io/topic/130848/read-and-store-the-checked-items-from-the-qtreewidget/3
What exactly is the problem with this?Regarding your other question: you can use https://doc.qt.io/qt-5/qtreewidgetitemiterator.html to iterate over all children of an item.
-
@jsulm my promblem is i am able to check the child item
but i want to check alll the child items just by checking the root item -
@kook So, did you check https://doc.qt.io/qt-5/qtreewidgetitemiterator.html ? It shows how to iterate over child elements...
-
@jsulm yes i read that document and tried using it by creating a simple code
QTreeWidgetItemIterator iterator(item); while (*iterator) { if ((*iterator)->checkState(0) == Qt::Checked) { qDebug()<< (*iterator)->text(0); } ++iterator; }
but i am unable to achieve want i wanted (checking all the child item by checking the root/parent) using this
what i am asking maybe a very simple thing but i am finding it difficult since i am a beginner please help me out thank you -
@kook you aren't changing anything in that loop. Don't you need to call
setCheckState
? See the documentation you have been pointed to; there is a very similar example. -
void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) { QTreeWidgetItemIterator iterator(item); while( *iterator) { if ( (*iterator)->childCount() != 0) { if ( (*iterator)->checkState(column) == Qt::Checked) { for (int i = 0; i < item->childCount(); ++i) { (*iterator)->child(i)->setCheckState(0, Qt::Checked); } } } ++iterator; }
ok i am able to do it now tell me is this the correct way of doing
-
@kook Why iterator->child?!
First, check the check state of the parent, then apply that state on the children.auto state = item->checkState(0); QTreeWidgetItemIterator iterator(item); while (*iterator) { qDebug()<< (*iterator)->text(0); (*iterator)->setCheckState(0, state); ++iterator; }
-
@jsulm thank you i will try this
-
@jsulm
Does the(*iterator)->setCheckState(0, state);
re-fire theon_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
for the child item's check state being changed? If so, good; if not, the OP will need a recursive function assuming he wants all descendants to get checked (and tree depth not limited to one level). -
@JonB said in changing the checkstate of all the child item by checking the root item in Qtree widget:
if not, the OP will need a recursive function assuming he wants all descendants to get checked
No, that's what QTreeWidgetItemIterator is doing.
-
@jsulm the reason i used (*iterator)->child is i have multiple root in the tree i want only that particular root's child to be checked not the whole tree
the solution you have given is doing the latter -
@kook The solution I gave you iterates over all children of an item, NOT the whole tree.
If you want to change only some children of an item then you need a condition to decide which one to change, right? What is this condition? Can you explain what exactly should happen? -
@jsulm
when i change the message1 state it should change only its children check state
i tried the code you provided its changing the whole tree check statei don't have any specific condition
-
@kook said in changing the checkstate of all the child item by checking the root item in Qtree widget:
i tried the code you provided its changing the whole tree check state
Then you did something wrong.
What do you pass as item in this line QTreeWidgetItemIterator iterator(item); ?
It needs to be the item user clicked (in your example message1).auto state = item->checkState(0); QTreeWidgetItemIterator iterator(item); while (*iterator) { qDebug()<< (*iterator)->text(0); (*iterator)->setCheckState(0, state); ++iterator; }
-
@jsulm i am adding the code inside this function
void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) { }
-
@kook You're right. This does not work as expected.
You will need to write a recursive method to iterate over all children of an item. -
@kook This works:
void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) for (int i = 0; i < item->childCount(); ++i) { qDebug() << item->child(i)->text(0); on_treeWidget_itemChanged(item->child(i), column); } }
-
@jsulm ok i will try thank you
-
@kook I just fixed my code...