PDA

View Full Version : How to delete items in QTreeWidget...



Patrick Sorcery
9th September 2010, 05:31
I'm using my classes & objects which inherit QTreeWidgetItem, and it crashes when I try to do remove an item in this way:


ui->treeWidget->currentItem()->parent()->removeChild(ui->treeWidget->currentItem());

While this way succeeded:


ui->treeWidget->invisibleRootItem()->removeChild(ui->treeWidget->currentItem());

But this can only remove top level items, and it still crashed when I try to remove the last one in treeWidget. What's more, in another project QTreeWidget::clear() made it crash, too... and I don't understand the documentation of this slot.

So, why it crashed, and how to delete an item from a QTreeWidget? Thanx!

e8johan
9th September 2010, 08:06
You remove it by deleting it, i.e. delete ui->treeWidget->currentItem().

Your crashed is probably since the removeChild method deletes the current item, which you refer to when finding the parent. I.e. you return into a function which no longer has a valid context.

Patrick Sorcery
9th September 2010, 09:33
Er~~(╯﹏╰)b it seems I forgot the simplest way......

But it still crashes when I try to delete all things in the treeWidget. Neither delete one by one nor QTreeWidget::clear() works:(

Lykurg
9th September 2010, 09:55
Debug your application and see where exactly your program crashes and post your back trace.

Patrick Sorcery
9th September 2010, 11:01
Er... I run with F5 and it tells me

"The inferior stopped because it received a signal from the Operating System.
Signal name: SIGSEGV
Signal meaning: Segmentation fault"

And... it must have crashed in QTreeWidgetItem::~QTreeWidgetItem(), but I don't know how to use debug to find out where it exactly is...

Lykurg
9th September 2010, 11:04
Assuming you are using Qt Creator. First build your application in debug mode, then start it via F5. Before closing the error dialog, show in the center bottom of creator, and there you will find the - graphical - back trace.

Patrick Sorcery
9th September 2010, 11:34
er... do u mean this which contains 43 lines? (in the attachment) I can understand nothing within...

Lykurg
9th September 2010, 11:58
as far as what I can see on your picture: in your slot on_treeWidget_it... you are accessing the text() function of an item, but you don't check the pointer to the item if it is not null. My guess. Show us the code of on_treeWidget_it...

Patrick Sorcery
9th September 2010, 12:25
Thanx a lot! I see what's wrong. There's a label in the project:


void MainWindow::on_treeWidget_itemSelectionChanged()
{
ui->label->setText( ui->treeWidget->currentItem()->text(0) );
}

it just show the name of which is selected. When nothing is available it crashes.

Then I tried this and failed again:


void MainWindow::on_treeWidget_itemSelectionChanged()
{
if(ui->treeWidget->topLevelItemCount()>0)
ui->label->setText( ui->treeWidget->currentItem()->text(0) );
}

I think when the treeWidget is empty all the members of it are unavailabe. But just how to keep it away from crashing if I still want to use this label?

Lykurg
9th September 2010, 12:27
That is not a valid check! You tree can have items but non must be selected! Use
if (ui->treeWidget->currentItem())
ui->label->setText( ui->treeWidget->currentItem()->text(0) );
else
ui->label->setText( "No item selected." );

Patrick Sorcery
10th September 2010, 03:21
Thanx! Finally it works!

Not a tough problem relly, but has taken me 3 days...

Lykurg
10th September 2010, 06:23
Not a tough problem relly, but has taken me 3 days...To avoid that and other common errors in future, I strongly suggest you to get familiar with the standard debugging and its output to realize what it is telling your. Just google for "gdb" (GNU debuger) and spend a half day of reading and you will save a lot of time later! And your state of health will also increase;)

E.g. your problem could have been solved withing 3 minutes (or even faster) without looking at lines which don't cause the problem. And mainly without changing irrelevant code and make it worser...

Bong.Da.City
10th September 2010, 09:41
You remove it by deleting it, i.e. delete ui->treeWidget->currentItem().

Your crashed is probably since the removeChild method deletes the current item, which you refer to when finding the parent. I.e. you return into a function which no longer has a valid context.

Thanks... This post help me for my ListWidget.. Now the Remove Button works :)
Thanks e8johan ;););)