c++ - QTreeWidgetItem : How can I get the selected item?

标签 c++ qt vector qt4 qtreewidget

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

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(item selected),重新分配 ID 列;因为每个实例都在该列中分配了一个编号。我在看这个 post ,三年前发布;但它主要只是指回我已经审查过的文档。另外,选择的答案似乎非常不确定,因为另一个答案似乎在正确的轨道上。我知道这个答案可能就在我面前;但我能说什么,菜鸟就是菜鸟,我很难理解实现。请只留下有成效的反馈,因为我只对学习和完成这项任务感兴趣。

最佳答案

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

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

selectionModelQAbstractItemModel 中。请注意 C++11 auto

关于c++ - QTreeWidgetItem : How can I get the selected item?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8974930/

相关文章:

c++ - 用随机数填充多个 vector

c++ - std::async 导致死锁?

python - 用python中的索引 reshape 向量

c++ - 停止 boost::asio 操作的规范方法

qt - 测试变量是否为空并在 qmake 期间给出默认值

qt - QT中信号和槽是怎么连接的?

c++ - 将 openCV .dll 文件添加到 Netbeans C++ Qt 应用程序

c++ - 如何从静态 vector 访问类元素?

c++ - 更改 QT 上的主窗口内容

c++ - 在 CLion 中使用 OpenGL 时出现未定义引用错误