Results 1 to 4 of 4

Thread: removing items QTreeWidget

  1. #1
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default removing items QTreeWidget

    Hey all,

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

    code below:
    Qt Code:
    1. QList<QTreeWidgetItem *> items;
    2.  
    3. items.append(new QTreeWidgetItem((QTreeWidget*)0,QStringList(QString(repoText))));//repoText = user-input text
    4. repoList->insertTopLevelItems(0, items); //repoList= my QTreeWidget
    To copy to clipboard, switch view to plain text mode 

    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:
    1. int x = items.indexOf(repoList->currentItem());
    2. repoList->takeTopLevelItem(x);
    To copy to clipboard, switch view to plain text mode 

    Ive also tried the following with no luck:
    Qt Code:
    1. //with the following 2, the program compiles fine but nothing happens when i choose an item and click the remove button.
    2. repoList->selectedItems().removeAt(x);
    3. repoList->currentItem()->takeChild(x);
    4.  
    5. //i get a bunch of errors using the following one...
    6. repoList->selectedItems().removeAll(repoList->currentItem()->text(0));
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: removing items QTreeWidget

    come on mates any help would be appriciated i have to manage this one way or another...

  3. #3
    Join Date
    Mar 2007
    Posts
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt/Embedded
    Platforms
    MacOS X Windows

    Default Re: removing items QTreeWidget

    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 :

    Qt Code:
    1. A_Zfun::A_Zfun(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. setupUi(this); // Just a treewidget and a button
    5.  
    6. QList<QTreeWidgetItem *> items;
    7.  
    8. for(int ii=0;ii<20;ii++)
    9. {
    10. QString userstuff = QString("Item nr %1").arg(ii+1);
    11. items.append(new QTreeWidgetItem((QTreeWidget*)0,QStringList(QString(userstuff))));
    12. }
    13. treeWidget->insertTopLevelItems(0, items);
    14. connect(DeleteSelected,SIGNAL(clicked()),this,SLOT(OnDeleteIt()));
    15. treeWidget->setCurrentItem(items.at(0));
    16. }
    17.  
    18. void A_Zfun::OnDeleteIt()
    19. {
    20. QTreeWidgetItem *item = treeWidget->currentItem();
    21. if(!item)return;
    22. int x = treeWidget->indexOfTopLevelItem(item);
    23. if(x >= 0 && x < treeWidget->topLevelItemCount())
    24. {
    25. item = treeWidget->takeTopLevelItem(x);
    26. if(item)delete item;
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows
    Wiki edits
    1

    Default Re: removing items QTreeWidget

    This worked for me.

    Qt Code:
    1. QTreeWidgetItem *item = treeWidget->currentItem();
    2. if(item)
    3. delete item->parent()->takeChild(item->parent()->indexOfChild(item));
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to phyatt for this useful post:

    Kryzon (26th March 2015)

Similar Threads

  1. Replies: 0
    Last Post: 18th September 2009, 15:10
  2. QTreeWidget Infinity Child items
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 14:19
  3. amount of items in QTreeWidget
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2008, 23:38
  4. How to update scene after removing items
    By nileshsince1980 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2007, 10:56
  5. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 13:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.