PDA

View Full Version : QTreeWidget child index



merlvingian
2nd October 2006, 01:22
My question is how do I obtain a childs position index of a TopLevelItem in a QTreeWidget so I can remove it?

I understand how to obtain the index of a toplevelitem and how I can use it to remove the entry from the tree. It also appears I can remove specific children of a toplevelitem by using something like



QList<QTreeWidgetItem *> selected = selectedItems();

QTreeWidgetItem *item = selected.first(); // first and only selected child item
QTreeWidgetItem *parent = item->parent(); // childs parent will always be a toplevelitem

parent->takeChild(**INDEX NEEDED**); // remove the child



There will only be one level of children on any given toplevelitem and it looks at this point I could use the child *item reference and iterate through the toplevelitem children until I find an item match so I can remove it, but this seems like an awkward way to solve the problem.

Is there something I am missing? Is there no way to remove a QTreeWidget item directly using it's QTreeWidgetItem reference? Searched forum and docs and still have not found a solution :confused:

Any help would be appreciated.

jpn
2nd October 2006, 05:35
QTreeWidgetItem::indexOfChild()

merlvingian
2nd October 2006, 17:42
Well atleast I asked the obvious question in the newbie section :D

The sad part is I went over those sections in the docs many times before I came and asked a stupid question and still missed it. Starting to think this ability to miss the openly obvious might be a medical condition.

Thank you for the help.

bmn
9th July 2015, 04:42
QList<QTreeWidgetItem *> selected = ui->twg->selectedItems();
QTreeWidgetItem *item = selected.first();
QTreeWidgetItem *parent = item->parent();
if(parent) {
QTreeWidgetItem *tmp = new QTreeWidgetItem(parent);
tmp->setText(0, "Dude");
parent->addChild(tmp);
}


QList<QTreeWidgetItem *> selected = ui->twg->selectedItems();
QTreeWidgetItem *item = selected.first();
QTreeWidgetItem *parent = item->parent();
if(parent) {
int index = parent->indexOfChild(item);
delete parent->takeChild(index);
}