[Qt-interest] removing items QTreeWidget

Malyushytsky, Alex alex at wai.com
Tue Nov 3 23:31:39 CET 2009


1) Do you really want just remove item from widget without deleting it?
If you want to delete item, and don't need it in the future, just delete it :P

QTreeWidgetItem *  item;
....
....
delete item; // that is it, item will be removed from the widget and deleted

2)  here is a function I am using if I want delete selection.
It requires you to specify the parent item ( which can be invisibleRootItem () if you want to start with root items) and depth to check (if you put the maximum number of children your root item can have, it will check everything. This value was required since I wanted to delete all items except specific depth if all the children are deleted)

I commented code which deletes Parents without children.


void deleteSelectedTreeViewItemChildren( QTreeWidgetItem* parentItem, int depth )
{
        QTreeWidgetItem* item= NULL;
        for (int i= parentItem->childCount()-1; i >-1; i--)
        {
                item = parentItem->child( i );

                if( item->isSelected () )
                {
                        delete item;
                }
                else
                {
                        if ( depth > 0 )
                        {
                                deleteSelectedTreeViewItemChildren( item, depth-1 );
//                              if( item->childCount()==0 )
//                              {
//                                      delete item;
//                              }
                        }
                }
        }

}

Hope this helps,
    Alex



From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of BRM
Sent: Tuesday, November 03, 2009 9:59 AM
To: Mystical Groovy
Cc: qt-interest at trolltech.com
Subject: Re: [Qt-interest] removing items QTreeWidget

Hint: When the user clicks the remove button, get the current selection and store it in another variable; then process that variable to do the actual remove.
Also, if removing multiple items, sort the list by reverse index - highest index to lowest index - so you don't have to keep retrieving the list of selected items.

Ben

P.S.  By doing the above you'll also make your code far more debuggable since you'll now have variables you control that you can monitor to see what
is being done - for example, you'll be able to spot the index values and whether or not it's even returning a valid index (it might not be) or if you're
using a function wrong. I know it's a stylistic thing - but believe me, it really helps to have code that supports debugging instead of code that looks
smart and short; you'll save yourself a lot of time in the long run. Just 2 cents.
________________________________________
From: Mystical Groovy <mysticalgr at gmail.com>
To: qt-interest at trolltech.com
Sent: Tue, November 3, 2009 12:29:48 PM
Subject: Re: [Qt-interest] removing items QTreeWidget

Anyone?
Ill have to manage this one way or another, so please help :)
2009/11/2 Mystical Groovy <mysticalgr at gmail.com>
Hey all,

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

code below:
Qt Code:



QList<QTreeWidgetItem *> items;

items.append(new QTreeWidgetItem((QTreeWidget*)0,QStringList(QString(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...)
Qt Code:

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

    repoList->takeTopLevelItem(x);
Ive also tried the following with no luck:
Qt Code:





//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 :_)




---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."

"Please consider our environment before printing this email."




More information about the Qt-interest-old mailing list