How to erase/delate a QTreeWidgetItem from a QTreeWidget?
Hello!
I'm trying for some time to find a way to remove an item from a QTreeWidget but I'm unsucessfull. Here is the way I add a item to the QTreeWidget:
Code:
{
itm->setText(0,data);
itm2->setText(0,semana);
itm2->setText(1,hora);
itm2->setText(2,smadesc);
itm->addChild(itm2);
}
No imagine that, in another function, I want to delete that itm2 or even itm. How do I do?
And second, regarding the metho removeChild(), I would like to know how can I say that that method which QTreeWidgetItem I want to remove if, as you may have seen in the previous code, I create and add a QTreeWidgetItem inside a method, not as a global variable.
Thanks!
Momergil
Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?
You retrieve a pointer to the item you wish to delete from the QTreeWidget methods like currentItem() or itemAbove() and then delete it. The item removes itself from the model of the QTreeWidget. You can also use that pointer in the removeChild() method of its parent.
Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?
Quote:
Originally Posted by
ChrisW67
You retrieve a pointer to the item you wish to delete from the QTreeWidget methods like currentItem() or itemAbove() and then delete it. The item removes itself from the model of the QTreeWidget. You can also use that pointer in the removeChild() method of its parent.
Thanks Chris for the help. The unique problem with this solution is that if there is a situation in which case a QTreeWidgetItem has some Childs and all of those childs are deleted, I would like for the "main" QTreeWidgetItem (the root as I called it) to be deleted as well, that means, when the last of its Childs are deleted. How can I do that?
Momergil
Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?
Design the logic and code it. There's no magic here. Before your code deletes an item check if its parent has more than one child (QTreeWidgetItem::childCount()). If it does then delete the child otherwise delete the parent instead (which will also delete the child). There is more than one way to achieve this: as an exercise try thinking of a variation.
Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?
Quote:
Originally Posted by
ChrisW67
Design the logic and code it. There's no magic here. Before your code deletes an item check if its parent has more than one child (
QTreeWidgetItem::childCount()). If it does then delete the child otherwise delete the parent instead (which will also delete the child). There is more than one way to achieve this: as an exercise try thinking of a variation.
Hello, Chris!
Thanks for the tip once again. Here is the code I implemented, which works fine:
Code:
void AlarmManagment::on_delete_2_clicked()
{
if (ui->Listofalarms->currentItem()->childCount() == 0)
{
if (ui->Listofalarms->currentItem()->parent()->childCount() == 1)
{
delete ui->Listofalarms->itemAbove(ui->Listofalarms->currentItem());
delete ui->Listofalarms->currentItem();
}
else
delete ui->Listofalarms->currentItem();
}
else
{
delete ui->Listofalarms->currentItem();
}
}
Thanks!
God bless,
Momergil