PDA

View Full Version : removing items QTreeWidget



Mystical Groovy
1st November 2009, 00:06
Hey all,

Im using QTreeWidget and a QList to display some create items from user-input text.

code below:


QList<QTreeWidgetItem *> items;

items.append(new QTreeWidgetItem((QTreeWidget*)0,QStringList(QStrin g(repoText))));//repoText = user-input text
repoList->insertTopLevelItems(0, items); //repoList= my QTreeWidget


now i have a remove button that when the user clicks it i want it to remove the currently selected item.

using the following code, items from the QTreeWidget are removed but not the one ive clicked! ("!?!WTF...)


int x = items.indexOf(repoList->currentItem());
repoList->takeTopLevelItem(x);


Ive also tried the following with no luck:


//with the following 2, the program compiles fine but nothing happens when i choose an item and click the remove button.
repoList->selectedItems().removeAt(x);
repoList->currentItem()->takeChild(x);

//i get a bunch of errors using the following one...
repoList->selectedItems().removeAll(repoList->currentItem()->text(0));


anyways, thank you for your time, and please help :_)

Mystical Groovy
2nd November 2009, 13:00
come on mates any help would be appriciated i have to manage this one way or another... :)

LaOnze2000
3rd November 2009, 22:54
I tried this with a widget composed of a treewidget and a button.
When you push the button, it will delete the item selected in the treewidget
See if this works for you :



A_Zfun::A_Zfun(QWidget *parent)
: QWidget(parent)
{
setupUi(this); // Just a treewidget and a button

QList<QTreeWidgetItem *> items;

for(int ii=0;ii<20;ii++)
{
QString userstuff = QString("Item nr %1").arg(ii+1);
items.append(new QTreeWidgetItem((QTreeWidget*)0,QStringList(QStrin g(userstuff))));
}
treeWidget->insertTopLevelItems(0, items);
connect(DeleteSelected,SIGNAL(clicked()),this,SLOT (OnDeleteIt()));
treeWidget->setCurrentItem(items.at(0));
}

void A_Zfun::OnDeleteIt()
{
QTreeWidgetItem *item = treeWidget->currentItem();
if(!item)return;
int x = treeWidget->indexOfTopLevelItem(item);
if(x >= 0 && x < treeWidget->topLevelItemCount())
{
item = treeWidget->takeTopLevelItem(x);
if(item)delete item;
}
}

phyatt
25th March 2015, 21:58
This worked for me.


QTreeWidgetItem *item = treeWidget->currentItem();
if(item)
delete item->parent()->takeChild(item->parent()->indexOfChild(item));