我目前是一名学生程序员,使用Qt在工作中构建GUI界面,我目前遇到了在Qt Documentation On the QTreeWidgetItem中找到解决方案的问题。我目前有一个界面,可以在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(选中项目),重新分配ID列;因为每个实例都在此列中分配了一个数字。我正在查看三年前发布的post;但它主要只是回顾我已经审查过的文档。此外,选择的答案似乎非常不确定,因为其他答案似乎已走上正轨。我明白这个答案可能就在我面前;但我能说什么,新手将是新手,我很难理解实施。请留下富有成效的反馈,因为我只对学习和完成这项任务感兴趣。提前致谢。
答案 0 :(得分:2)
您需要获取selectionModel
,然后获取所选索引,然后迭代它们:
treeWidget->selectionModel()->selection();
auto idx = sel.indexes();
foreach(auto index, idx) {
camModel_->removeRow(index.row());
}
}
selectionModel
位于QAbstractItemModel
。请注意C ++ 11 auto
。