gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:30:03 31 4
gpt4 key购买 nike

我目前是一名学生程序员,在工作中使用 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com