Results 1 to 5 of 5

Thread: How to erase/delate a QTreeWidgetItem from a QTreeWidget?

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to erase/delate a QTreeWidgetItem from a QTreeWidget?

    Hello!

    I'm trying for some time to find a way to remove an item from a QTreeWidget but I'm unsucessfull. Here is the way I add a item to the QTreeWidget:

    Qt Code:
    1. void AlarmManagment::addRoot(QString data, QString semana, QString hora, QString smadesc)
    2. {
    3. QTreeWidgetItem *itm = new QTreeWidgetItem(ui->Listofalarms);
    4. itm->setText(0,data);
    5.  
    6. itm2->setText(0,semana);
    7. itm2->setText(1,hora);
    8. itm2->setText(2,smadesc);
    9.  
    10. itm->addChild(itm2);
    11. }
    To copy to clipboard, switch view to plain text mode 

    No imagine that, in another function, I want to delete that itm2 or even itm. How do I do?

    And second, regarding the metho removeChild(), I would like to know how can I say that that method which QTreeWidgetItem I want to remove if, as you may have seen in the previous code, I create and add a QTreeWidgetItem inside a method, not as a global variable.


    Thanks!

    Momergil

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?

    You retrieve a pointer to the item you wish to delete from the QTreeWidget methods like currentItem() or itemAbove() and then delete it. The item removes itself from the model of the QTreeWidget. You can also use that pointer in the removeChild() method of its parent.

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?

    Quote Originally Posted by ChrisW67 View Post
    You retrieve a pointer to the item you wish to delete from the QTreeWidget methods like currentItem() or itemAbove() and then delete it. The item removes itself from the model of the QTreeWidget. You can also use that pointer in the removeChild() method of its parent.

    Thanks Chris for the help. The unique problem with this solution is that if there is a situation in which case a QTreeWidgetItem has some Childs and all of those childs are deleted, I would like for the "main" QTreeWidgetItem (the root as I called it) to be deleted as well, that means, when the last of its Childs are deleted. How can I do that?


    Momergil

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?

    Design the logic and code it. There's no magic here. Before your code deletes an item check if its parent has more than one child (QTreeWidgetItem::childCount()). If it does then delete the child otherwise delete the parent instead (which will also delete the child). There is more than one way to achieve this: as an exercise try thinking of a variation.

  5. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to erase/delate a QTreeWidgetItem from a QTreeWidget?

    Quote Originally Posted by ChrisW67 View Post
    Design the logic and code it. There's no magic here. Before your code deletes an item check if its parent has more than one child (QTreeWidgetItem::childCount()). If it does then delete the child otherwise delete the parent instead (which will also delete the child). There is more than one way to achieve this: as an exercise try thinking of a variation.
    Hello, Chris!

    Thanks for the tip once again. Here is the code I implemented, which works fine:

    Qt Code:
    1. void AlarmManagment::on_delete_2_clicked()
    2. {
    3. if (ui->Listofalarms->currentItem()->childCount() == 0)
    4. {
    5. if (ui->Listofalarms->currentItem()->parent()->childCount() == 1)
    6. {
    7. delete ui->Listofalarms->itemAbove(ui->Listofalarms->currentItem());
    8. delete ui->Listofalarms->currentItem();
    9. }
    10. else
    11. delete ui->Listofalarms->currentItem();
    12. }
    13. else
    14. {
    15. delete ui->Listofalarms->currentItem();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

    God bless,


    Momergil

Similar Threads

  1. QTreeWidget::setCurrentItem ( QTreeWidgetItem * item )
    By gboelter in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2011, 22:21
  2. Unable to remove QTreeWidgetItem row from Qtreewidget
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 2
    Last Post: 1st December 2010, 05:40
  3. Customizing QTreeWidget and QTreeWidgetItem
    By mots_g in forum Qt Programming
    Replies: 0
    Last Post: 18th August 2010, 06:53
  4. Cursor is not moving through QTreeWidgetItem list in QTreeWidget
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 0
    Last Post: 11th December 2009, 06:52
  5. Replies: 2
    Last Post: 24th May 2009, 10:27

Tags for this Thread

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.