QTreeWidget QCheckBox signal itemChanged for root
-
Hi,
I'm want to implement tree of checkbox.
I used tree widget.when some root is change, I want to update all the childes,
but when I update some child check state, the signal fire again.when one child is changed , it work partly ok, when i need to change to root (let say if all is selected and i uncheck one child so i need to uncheck the root and i got the same problem)
is there is a good example or explain a good way to implement it??
-
Updating the check state will always fire the signal, unless you block signals before programmatic update and restore the original state afterwards. You can do that manually or with a signal blocker, see here.
-
@Axel-Spoerl said in QTreeWidget QCheckBox signal itemChanged for root:
u can do that m
can you offer a solution to this fetcher? (tree of checkboxes)
when you check/uncheck root - update all the children as the root
if a child is checked and it's the last that not checked - set the root as checked (and the same logic for unchecking). -
@Lior
What do you mean by „this fetcher“?
Post your code and the community will look what can be improved. -
Hi,
Here is the example code. it is quick and dirty only for learning this, I will rewrite it,
the main objective is to hold a tree of checkboxes,- when a checkbox is checked or unchecked - do something
- if a root is checked or unchecked - update all the children
- if all the children are checked, update the root to be checked
- if at least one of the children is not checked, the root is not checked
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPushButton> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->treeWidget->setColumnCount(1); // set headers QStringList label ; label << "FileName";//<< "FilePath"<< "Widget"; ui->treeWidget->setHeaderLabels(label); QTreeWidgetItem *root = new QTreeWidgetItem(ui->treeWidget); root->setText(0,"RootFile"); root->setCheckState(0, Qt::Unchecked); ui->treeWidget->addTopLevelItem(root); addItem(0, "c" + QString::number(1), Qt::Unchecked, root); addItem(0, "c" + QString::number(2), Qt::Unchecked, root); addItem(0, "c" + QString::number(3), Qt::Unchecked, root); addItem(0, "c" + QString::number(4), Qt::Unchecked, root); } void MainWindow::addItem(int col, QString name, Qt::CheckState checkedState, QTreeWidgetItem *root) { CPloyPropertiesTreeWidgetItem *child = new CPloyPropertiesTreeWidgetItem(); child->setText(col,name); child->setCheckState(col, checkedState); root->addChild(child); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) { bool isRootChanged = item->parent() == nullptr; if(!isRootChanged) { Qt::CheckState cs = item->checkState(column); QTreeWidgetItemIterator it(item->parent()); bool isAll = true; ++it; // skip the root while (*it) { // if at least one is different, uncheck the root, otherwise set the root as the child if ((*it)->checkState(column) != cs) { isAll = false; break; } ++it; } if(isAll) { item->parent()->setCheckState(column,cs); } else { item->parent()->setCheckState(column,Qt::CheckState::Unchecked); } } if(isRootChanged) { QTreeWidgetItemIterator it(item); Qt::CheckState rootCs = item->checkState(column); ++it; while (*it) { if ((*it)->checkState(column) != rootCs) { (*it)->setCheckState(column, rootCs); } ++it; } } else // child changed { enableDisableVisablity(); } } void MainWindow::enableDisableVisablity() { }