Search code examples
linuxqtqtreewidget

How to select multiple items in treewidget and delete those items?


When I am selecting items in treewidget and then clicked on delete button then is is just deleting top most item only from treewidget
Can you correct this code?
This function is calling in connect statement while clicking on button

void TableDockWidget::deleteGroup() {

QTreeWidgetItem *item = treeWidget->currentItem();

QTreeWidgetItem* nextItem = treeWidget->itemBelow(item);

if ( item == NULL ) return;

PeakGroup* group = getSelectedGroup(); //this function is using to                        //select items from treewidget

if ( group == NULL ) return;

PeakGroup* parentGroup = group->parent;

if ( parentGroup == NULL ) { //top level item

    deleteGroup(group); //this is using to delete group
} else if ( parentGroup && parentGroup->childCount() ) {    //this a child item
    if ( parentGroup->deleteChild(group) ) {
        QTreeWidgetItem* parentItem = item->parent();
        if ( parentItem ) {
            parentItem->removeChild(item);
            delete(item);
        }
    }
}
//show NextItem
if(nextItem) treeWidget->setCurrentItem(nextItem,0);
return;

}

//here, code of function which is selecting items.

PeakGroup* TableDockWidget::getSelectedGroup() { 
QTreeWidgetItem *item = treeWidget->currentItem();
if (!item) return NULL;
QVariant v = item->data(0,Qt::UserRole);
PeakGroup*  group =  v.value<PeakGroup*>();
if ( group != NULL ) { return group; }
return NULL;

}

// code of function which is deleting group

void TableDockWidget::deleteGroup(PeakGroup *groupX) {
qDebug() << "TableDockWidget::deleteGroup()";
if(!groupX) return;

int pos=-1;
for(int i=0; i < allgroups.size(); i++) {
    if ( &allgroups[i] == groupX ) {
        pos=i; break;
    }
}
if (pos == -1) return;

//qDebug() << "Delete pos=" << pos;
QTreeWidgetItemIterator it(treeWidget);
while (*it) {
    QTreeWidgetItem* item = (*it);
    if (item->isHidden()) { ++it; continue; }
    QVariant v = item->data(0,Qt::UserRole);
    PeakGroup*  group =  v.value<PeakGroup*>();
    if ( group != NULL and group == groupX) {
        item->setHidden(true);

        //Deleteing
        allgroups.erase(allgroups.begin()+pos);
        int posTree = treeWidget->indexOfTopLevelItem(item);
        if (posTree != -1) treeWidget->takeTopLevelItem(posTree);
        break;
    }
    ++it;
}

for(unsigned int i = 0; i < allgroups.size(); i++) {
    allgroups[i].groupId = i + 1;
}
updateTable();
_mainwindow->getEicWidget()->replotForced();

}


Solution

  • PeakGroup* TableDockWidget::getSelectedGroup() { 
    PeakGroup*  group;
    QList<QTreeWidgetItem*>selected = treeWidget->selectedItems();
    if(selected.size() == 0) return NULL;
    Q_FOREACH (QTreeWidgetItem* item, selected) {
              QVariant v = item->data(0,Qt::UserRole);
               group =  v.value<PeakGroup*>();
              item->setHidden(true);
           }
    if ( group != NULL ) { return group; }
    else
    return NULL;
    }