C++ (Qt)/// Где-тоconnect(treePhaseView, SIGNAL(itemPressed(QTreeWidgetItem*, int)), this, SLOT(treeItemPress(QTreeWidgetItem*, int))); /// Нажатие на элемент дереваvoid TLV::treeItemPress(QTreeWidgetItem* item, int column){ if(item == 0) return; for(register int i=0; i<item -> childCount(); ++i) { QTreeWidgetItem* childItem = item -> child(i); if(childItem == 0) continue; Qt::CheckState check = (childItem -> checkState(column) == 2 || childItem -> checkState(column) == 1) ? Qt::Unchecked : Qt::Checked; childItem -> setCheckState(column, check); treeItemPress(childItem, column); } QTreeWidgetItem* parentItem = item -> parent(); if(parentItem == 0) return; Qt::CheckState check = (parentItem -> checkState(column) == 2 || parentItem -> checkState(column) == 1) ? Qt::Unchecked : Qt::Checked; parentItem -> setCheckState(column, check);}
C++ (Qt)...QTreeWidget *tree = new QTreeWidget;tree->setColumnCount(1); QTreeWidgetItem *root = new QTreeWidgetItem(QStringList(QString("root")));root->setCheckState(0, Qt::Unchecked);tree->insertTopLevelItem(0, root); for (int i = 0; i < 10; ++i) { QTreeWidgetItem *item = new QTreeWidgetItem(root, QStringList(QString("item: %1").arg(i))); item->setCheckState(0, Qt::Unchecked); for (int j = 0; j < 10; ++j) { QTreeWidgetItem *itm = new QTreeWidgetItem(item, QStringList(QString("item: %1").arg(i))); itm->setCheckState(0, Qt::Unchecked); } } connect(tree, SIGNAL(itemChanged(QTreeWidgetItem *, int)), SLOT(itemChanged(QTreeWidgetItem *, int)));....void MyWidget::itemChanged(QTreeWidgetItem *item, int){ changeItemState(item);} void MyWidget::changeItemState(QTreeWidgetItem *item){ if (!item) return; for (int i = 0; i < item->childCount(); ++i) { QTreeWidgetItem *child = item->child(i); child->setCheckState(0, item->checkState(0)); changeItemState(child); }}
C++ (Qt)for(register int i=0; i<item -> childCount(); ++i) { QTreeWidgetItem* childItem = item -> child(i); if(childItem == 0) continue; Qt::CheckState check = (childItem -> checkState(column) == 2 || childItem -> checkState(column) == 1) ? Qt::Unchecked : Qt::Checked; childItem -> setCheckState(column, check); treeItemPress(childItem, column); }
C++ (Qt)QTreeWidgetItemIterator it(item);while (*it) { Qt::CheckState check = ((*it) -> checkState(column) == 2 || (*it) -> checkState(column) == 1) ? Qt::Unchecked : Qt::Checked; (*it)->setCheckState(column, check); ++it;}
C++ (Qt)QTreeWidgetItemIterator it(item);while (*it) { Qt::CheckState check = ((*it) -> checkState(0) == Qt::Checked) ? Qt::Unchecked : Qt::Checked; (*it)->setCheckState(0, check); ++it;}
C++ (Qt)пишу setCheckState(column, Qt::Unchecked); // или же Checked
C++ (Qt) item -> checkState(column)
C++ (Qt)/// Нажатие на элемент дереваvoid TLV::treeItemPress(QTreeWidgetItem* item, int column){ if(item == 0) return; Qt::CheckState check((item -> checkState(column) == Qt::Checked) ? Qt::Unchecked : Qt::Checked); for(register int i=0; i<item -> childCount(); ++i) { QTreeWidgetItem* childItem = item -> child(i); if(childItem == 0) continue; childItem -> setCheckState(column, check); treeItemPress(childItem, column); } QTreeWidgetItem* parentItem = item -> parent(); if(parentItem == 0) return; parentItem -> setCheckState(column, check);}
/// Нажатие на элемент дереваvoid TLV::treeItemPress(QTreeWidgetItem* item, int column){ if(item == 0) return; Qt::CheckState check((item -> checkState(column) == Qt::Checked) ? Qt::Unchecked : Qt::Checked); QTreeWidgetItemIterator it(item); while(*it) { (*it) -> setCheckState(column, check); ++it; }}