c++ 四叉树控件项:如何获取所选项目?

f1tvaqid  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(118)

我目前是一名学生程序员,在工作中使用Qt构建GUI界面,我目前遇到了一个问题,在QTreeWidgetItem上的Qt文档中找到解决方案。我目前有一个界面,其中有编辑,删除,和复制示例。了解我的树的填充方式对您可能很重要。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中删除。我的假设是识别选择了哪一行,确认删除,删除itemAt(选择的项),重新分配ID列;因为每个示例在这一列中被分配了一个编号。我看了这个post,三年前发布的;但它主要只是指回我已经审查过的文档。另外,选择的答案似乎非常不确定,因为另一个答案似乎已经走上了正确的轨道。我明白这个答案可能就在我面前;但是我能说什么呢,菜鸟就是菜鸟,我很难理解它的实现。请只留下富有成效的反馈,因为我只对学习和完成这项任务感兴趣。

pod7payv

pod7payv1#

您需要获取selectionModel,然后是选定的索引,然后迭代它们:

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

QAbstractItemModel中。注意C++11 auto

相关问题