You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, May 7th 2009, 2:28pm

clearing a QTreeWidget

Going directly to the point,
when I try to:

Source code

1
ui.treeWidget->takeTopLevelItem(ui.treeWidget->indexOfTopLevelItem(ui.treeWidget->currentItem()));

(yes, very generic)

to remove the current selected TopLevelItem in a QTreeWidget, it works fine for each of the TopLevelItems, except for the very last item (or first, actually), and goes runtime error.
also, when I try to use

Source code

1
ui.treeWidget->clear();


it goes runtime error as well...
why's that?
thank you


PS: oh, yeah, I didn't use removeItem from QTreeWidget API because it simply didn't work. I must have something wrong.

This post has been edited 1 times, last edit by "Lyucs" (May 7th 2009, 2:42pm)


Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Thursday, May 7th 2009, 8:45pm

Lyucs,

Yep, seen this before a while back; attached is an example that I tend to fall back on to see how I got around this as well.

Probably would work for you just by adding:

delete ui.treeWidget->takeTopLevelItem(ui.treeWidget->indexOfTopLevelItem(ui.treeWidget->currentItem()));

Not the cleanest method but it get the job done.

Junior
Junior has attached the following file:

3

Friday, May 8th 2009, 3:25pm

Hi Junior,

I don't get why we have to call "delete".
well, anyways, I got a brand new fresh problem. I gave up on just deleting it, I want to return it. here goes my function:

/* Removes the current item from the TreeWidget, and returns it */ QTreeWidgetItem *Editor::removeItemTree() {
QTreeWidgetItem *currentParent = ui.treeWidget->currentItem()->parent();
QTreeWidgetItem *currentItem = ui.treeWidget->currentItem();


/* if the current item has a parent (it's not a TopLevelItem) */
if (currentParent)
return currentParent->takeChild(currentParent->indexOfChild(currentItem));
/* if the current item does not have a parent (it IS a TopLevelItem) */
else
{
rootItem = ui.treeWidget->invisibleRootItem();
return rootItem->takeChild(rootItem->indexOfChild(currentItem));
}
}


this doesn't work either.
I tried your piece of code, and it didn't work, that must be because we have some thing different on our codes. I'll check it throughoutly.

but what amazes me is that ui.treeWidget->clear(); DOES NOT WORK! how the heck?

PS: oh, now I know why...:
from QTreeWidget API:
"Note: Since each item is removed from the tree widget before being deleted, the return value of QTreeWidgetItem::treeWidget() will be invalid when called from an item's destructor."

it tries to remove the last item... and fails miserably too... dang, what to do...?

This post has been edited 1 times, last edit by "Lyucs" (May 8th 2009, 3:30pm)