PDA

View Full Version : How to erase/delate a QTreeWidgetItem from a QTreeWidget?



Momergil
19th September 2011, 18:24
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:



void AlarmManagment::addRoot(QString data, QString semana, QString hora, QString smadesc)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->Listofalarms);
itm->setText(0,data);

QTreeWidgetItem *itm2 = new QTreeWidgetItem();
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

ChrisW67
20th September 2011, 04:37
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.

Momergil
21st September 2011, 20:01
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

ChrisW67
22nd September 2011, 00:42
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.

Momergil
22nd September 2011, 19:37
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:



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