1

我目前是一名学生程序员,在工作中使用 Qt 构建 GUI 界面,目前在 QTreeWidgetItem 上的 Qt 文档中找到解决方案时遇到问题。我目前有一个界面,其中包含用于编辑、删除和复制我的QTree. 了解我的树的填充方式可能对您很重要。中显示的项目QTree以这种方式从向量中动态附加。

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);
                    }
            }
}

现在我真的需要一种方法来找出用户在其中选择了哪个项目,QTree以便我可以将其从 Vector 中删除。我的psuedo是识别哪一行被选中,确认删除,删除itemAt(item selected),重新分配ID列;因为每个实例都在此列中分配了一个编号。我在看这个帖子,三年前发布;但它主要是指回我已经审查过的文档。另外,选择的答案似乎非常不确定,因为其他答案似乎一直在正确的轨道上。我知道这个答案可能就在我面前;但我能说什么,菜鸟将是菜鸟,我很难理解实施。请只留下富有成效的反馈,因为我只对学习和完成这项任务感兴趣。提前致谢。

4

1 回答 1

2

您需要获取selectionModel,然后是选定的索引,然后对其进行迭代:

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

selectionModelQAbstractItemModel. 注意 C++11 auto

于 2012-01-23T17:03:11.737 回答