c++qtvectorqt4qtreewidget

QTreeWidgetItem: How can I get the selected item?


I am currently a student programmer using Qt to build a GUI inteface at work and I am currently running into a problem finding a solution in the Qt Documentation On the QTreeWidgetItem. I currently have an interface that has buttons to edit, delete, and copy instances in my QTree. It might be important for you to understand the way my tree is populated. The items displayed in the QTree are dynamically appended from a vector in this way.

void InjectionGUI::addInjections_Clicked() //creates a new instance of injections
{
    InjectionDialog newAddInjectionDialog; //where my dialog opens for user input
    InjectionData defaultValues; 
    newAddInjectionDialog.setData(defaultValues);
            if(newAddInjectionDialog.exec() == QDialog::Accepted)//a check data returns either Accepted or rejected 
            {
               qTableInjectionData.append(newAddInjectionDialog.transInjectionData); //this appends the valid data from the injection dialog to the vector qTableInjectionData
                ui->injectionTreeWidget->clear();
                for (int i=0; i < qTableInjectionData.size(); i++) // here I add the data from the vector to the tree widget.
                    {
                    InjectionData temp = qTableInjectionData.at(i);
                        QString injectionType;
                        QString tmpStr;
                        int column = 0;

                        //need sorting solution(still working on this)
                        if(temp.particleInjectionActive == true) // this evaluates the injection types
                        {
                            if(temp.particleInjectionOrLiquidDroplets == true)
                            {
                                injectionType += "(LD)";
                            }
                            else
                            {
                                injectionType += "(P)";
                            }
                        }
                        if(temp.fluidInjectionActive == true)
                        {
                            injectionType += "(F)";
                        }
                        QTreeWidgetItem *qTreeWidgetItemInjectionData = new QTreeWidgetItem(ui->injectionTreeWidget); //Here data is added into rows from each instance of injection dialog found in vector
                        qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(i));
                        qTreeWidgetItemInjectionData->setText(column++, temp.lineEditInjectionName);
                        qTreeWidgetItemInjectionData->setText(column++, injectionType);
                        qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditParitcleVelocity));
                        qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditFluidVelocity));
                        qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditParticleMassFlow));
                        qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditFluidMassFlow));
                        qTreeWidgetItemInjectionData->setText(column++, temp.lineEditComment);
                    }
            }
}

Now I really need a way to find out which item the user has selected in the QTree so that I can have it deleted from the Vector. My psuedo is to identify which row has been selected, confirm delete, delete itemAt(item selected), reassign ID column; because each instance is assigned a number in this column. I was looking over this post, posted three years ago; but it mostly just refers back to the documentation that I have already been reviewing. Plus, the answer that was selected seems extremely inconclusive as the other answer seem to have been on the right track. I understand that this answer might be right in front of me here; but what can i say, noobs will be noobs and I am having a hard time understanding the implementation. Please only leave productive feedback as I am only interested in learning and accomplishing this task.


Solution

  • You need to obtain the selectionModel, then the selected indices and then iterate over them:

    treeWidget->selectionModel()->selection();
      auto idx = sel.indexes();
      foreach(auto index, idx) {
        camModel_->removeRow(index.row());
      }
    }
    

    selectionModel is in QAbstractItemModel. Note the C++11 auto.