void ImageViewer::TreeWidget(){ dockWidget = new QDockWidget(tr("Tree"), this); dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); addDockWidget(Qt::LeftDockWidgetArea, dockWidget); QTreeWidget *treeWidget = new QTreeWidget(dockWidget); treeWidget->setColumnCount(2); QStringList headers; headers << ("Subject") << ("Default"); treeWidget->setHeaderLabels(headers); QTreeWidgetItem *Main = new QTreeWidgetItem(treeWidget, QStringList("Main1")); for( int m = 0; m <10; m++) { QTreeWidgetItem *pChild = new QTreeWidgetItem( Main ); QString sz00 = QObject::tr( "first defects: %1 ").arg( m); pChild->setText(0, sz00); POP.push_back(pChild); } QTreeWidgetItem *Main2 = new QTreeWidgetItem(treeWidget, QStringList("Main2")); for( int n = 0; n <10; n++) { QTreeWidgetItem *pChild1 = new QTreeWidgetItem( Main2 ); QString sz00 = QObject::tr( "second defects: %1 ").arg( n ); pChild1->setText(0, sz00); POP.push_back(pChild1); } qDebug() << "Iterating from top level"; for (QTreeWidgetItemIterator it(treeWidget); *it; ++it ) qDebug() << "Item text" << (*it)->text(0); dockWidget->setWidget(treeWidget);connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)), this, SLOT(setActivated(QTreeWidgetItem*, int)));}void ImageViewer::setActivated(QTreeWidgetItem *item, int column){ qDebug() << "Work2"; qDebug() << "Number:" << column;}
item->parent->indexOfChild(item)
void ImageViewer::setActivated(QTreeWidgetItem *item, int column){int i;i=item->parent()->indexOfChild(item); for (QTreeWidgetItemIterator it(item, QTreeWidgetItemIterator::Selected); *it; ++it ) //обход выделенных item qDebug() << "Select Item " << (*it)->text(0);qDebug() << "Number:" << i;}