#ifndef OBJECT_TREE_MODEL_H#define OBJECT_TREE_MODEL_H#include <QStandardItemModel>class ObjectTreeModel : public QStandardItemModel{ Q_OBJECTpublic: ObjectTreeModel( QObject* parent = 0 );protected slots: void onItemChanged( QStandardItem* item );};#endif // OBJECT_TREE_MODEL_H
#include "objecttreemodel.h"ObjectTreeModel::ObjectTreeModel( QObject* parent ) : QStandardItemModel( parent ){ connect( this, SIGNAL(itemChanged(QStandardItem*)), SLOT(onItemChanged(QStandardItem*)) );}void ObjectTreeModel::onItemChanged( QStandardItem* item ){ if ( !item->isCheckable() || item->column() != 0 ) return; Qt::CheckState state = item->checkState(); if ( state == Qt::Checked || state == Qt::Unchecked ) { for ( int i = 0; i < item->rowCount(); i++ ) { QStandardItem* child = item->child(i); if ( child->isCheckable() && child->checkState() != state ) child->setCheckState(state); } } QStandardItem* parent = item->parent(); if ( parent && parent->isCheckable() ) { state = parent->child(0)->checkState(); if ( state == Qt::PartiallyChecked ) parent->setCheckState( state ); else { int i = 1; while ( i < parent->rowCount() && parent->child(i)->checkState() == state ) i++; if ( i != parent->rowCount() ) state = Qt::PartiallyChecked; parent->setCheckState( state ); } }}
C++ (Qt)/// Нажатие на элемент дереваvoid TLV::itemPress(QTreeWidgetItem* item, int column){ if(item == 0) return; Qt::CheckState state((item -> checkState(column) == Qt::Checked) ? Qt::Unchecked : Qt::Checked); for(register int i=0; i<item -> childCount(); ++i) { QTreeWidgetItem* child = item -> child(i); if(child == 0) continue; if(child -> checkState(column) != state) child -> setCheckState(column, state); for(register int j=0; j<child -> childCount(); ++j) { QTreeWidgetItem* grandchild = child -> child(j); if(grandchild == 0) continue; if(grandchild -> checkState(column) != state) grandchild -> setCheckState(column, state); } } QTreeWidgetItem* parent = item -> parent(); if(parent == 0) return; parent -> setCheckState(column, state);}
C++ (Qt)/// Нажатие на элемент дереваvoid TLV::itemPress(QTreeWidgetItem* item, int column){ if(item == 0) return; Qt::CheckState state((item -> checkState(column) == Qt::Checked) ? Qt::Unchecked : Qt::Checked); for(register int i=0; i<item -> childCount(); ++i) { QTreeWidgetItem* child = item -> child(i); if(child == 0) continue; if(child -> checkState(column) != state) child -> setCheckState(column, state); for(register int j=0; j<child -> childCount(); ++j) { QTreeWidgetItem* grandchild = child -> child(j); if(grandchild == 0) continue; if(grandchild -> checkState(column) != state) grandchild -> setCheckState(column, state); } } QTreeWidgetItem* parent = item -> parent(); if(parent == 0) return; bool all = true; for(register int i=0; i<parent -> childCount(); ++i) { QTreeWidgetItem* child = parent -> child(i); if(child == 0) break; if(item != child && child -> checkState(column) != Qt::Unchecked) { all = false; break; } } if(all) parent -> setCheckState(column, state);}
C++ (Qt)void TreeWidget::treeItemPress(QTreeWidgetItem *item, int column){ if (!item) return; Qt::CheckState check = item->checkState(0); QList<QTreeWidgetItem *> items; items << item; while (!items.isEmpty()) { QTreeWidgetItem *itm = items.takeFirst(); for (int i = 0; i < itm->childCount(); ++i) { QTreeWidgetItem *child = itm->child(i); child->setCheckState(0, check); items << child; } } QTreeWidgetItem* parent = item -> parent(); if(parent == 0) return; bool all = true; for(register int i=0; i<parent -> childCount(); ++i) { QTreeWidgetItem* child = parent -> child(i); if(child == 0) break; if(item != child && child -> checkState(column) != Qt::Unchecked) { all = false; break; } } if(all) parent -> setCheckState(column, check);}