Solved changing the checkstate of all the child item by checking the root item in Qtree widget
-
@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...
-
@jsulm its not changing the checked state of children
-
@kook said in changing the checkstate of all the child item by checking the root item in Qtree widget:
its not changing the checked state of children
Come on, that's because I did not add the code to change the checked state. Add that part:
void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) for (int i = 0; i < item->childCount(); ++i) { qDebug() << item->child(i)->text(0); item->child(i)->setCheckState(0, item->checkState(0)); on_treeWidget_itemChanged(item->child(i), column); } }
-
@jsulm sorry i added that part its working
thank you for your time and solution