PDA

View Full Version : a simple question .how to delete a treewidgetitem



Alina
13th February 2006, 02:21
I have a treewidget in a dock window . i want to know how can i delete the activated treewidgetitem,and then show the new treewidget in dock?

yogeshm02
13th February 2006, 06:03
I have a treewidget in a dock window . i want to know how can i delete the activated treewidgetitem,and then show the new treewidget in dock?



//TIP: Following code is only fabricated by looking at the docs, i have not tested it.
if(treeWidget->currentItem() && !treeWidget->currentItem()->parent()){
delete treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(treeWidget->currentItem()));
}else if(treeWidget->currentItem() && treeWidget->currentItem()->parent()){
delete treeWidget->currentItem()->parent()->takeChild(treeWidget->currentItem()->parent()->indexOfChild(treeWidget->currentItem());
}


One this is done, QTreeWidget should update it accordingly.

Hope this works

Alina
17th February 2006, 04:34
//TIP: Following code is only fabricated by looking at the docs, i have not tested it.
if(treeWidget->currentItem() && !treeWidget->currentItem()->parent()){
delete treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(treeWidget->currentItem()));
}else if(treeWidget->currentItem() && treeWidget->currentItem()->parent()){
delete treeWidget->currentItem()->parent()->takeChild(treeWidget->currentItem()->parent()->indexOfChild(treeWidget->currentItem());
}


One this is done, QTreeWidget should update it accordingly.

Hope this works
how can a treewidget get the currentItem? when click a item ,I use selecteditems() or
currentitem() ,both hava no return . i want to delete a clicked treewidetitem ,but i do not
know how to get it.

Mad Max
17th February 2006, 05:01
how can a treewidget get the currentItem? when click a item ,I use selecteditems() or
currentitem() ,both hava no return . i want to delete a clicked treewidetitem ,but i do not
know how to get it.
:) This method do exist, I assure you:
http://doc.trolltech.com/4.1/qtreewidget.html#currentItem

PS. yogeshm02, I think here there is need to reformat the example code to more suitable kind.

yogeshm02
17th February 2006, 14:34
Do not just try to use the code given by me. Try to understand what is being done, and then you would have no questions. ;) Refer to qt docs.



PS. yogeshm02, I think here there is need to reformat the example code to more suitable kind.
I wanted to pack-up in as few lines as possible. :cool:

Alina
20th February 2006, 09:40
Do not just try to use the code given by me. Try to understand what is being done, and then you would have no questions. ;) Refer to qt docs.


I wanted to pack-up in as few lines as possible. :cool:

Thank your help !
now I know your code about how to delete a treewidgetitem. but now my question is
how can I get the treewidgetitem which the mouse has clicked?
I just want to realize the follow operation: click a treewidgetitem and then press a delete button to delete the clicked item.

zlatko
20th February 2006, 11:18
You can sublass QTreeWidget and reinplement keyPressEvent ( QKeyEvent * )
:rolleyes:

or..try use eventFilter() for main widget