Results 1 to 4 of 4

Thread: QTreeWidget child index

  1. #1
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeWidget child index

    My question is how do I obtain a childs position index of a TopLevelItem in a QTreeWidget so I can remove it?

    I understand how to obtain the index of a toplevelitem and how I can use it to remove the entry from the tree. It also appears I can remove specific children of a toplevelitem by using something like

    Qt Code:
    1. QList<QTreeWidgetItem *> selected = selectedItems();
    2.  
    3. QTreeWidgetItem *item = selected.first(); // first and only selected child item
    4. QTreeWidgetItem *parent = item->parent(); // childs parent will always be a toplevelitem
    5.  
    6. parent->takeChild(**INDEX NEEDED**); // remove the child
    To copy to clipboard, switch view to plain text mode 

    There will only be one level of children on any given toplevelitem and it looks at this point I could use the child *item reference and iterate through the toplevelitem children until I find an item match so I can remove it, but this seems like an awkward way to solve the problem.

    Is there something I am missing? Is there no way to remove a QTreeWidget item directly using it's QTreeWidgetItem reference? Searched forum and docs and still have not found a solution

    Any help would be appreciated.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget child index

    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    merlvingian (2nd October 2006)

  4. #3
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget child index

    Well atleast I asked the obvious question in the newbie section

    The sad part is I went over those sections in the docs many times before I came and asked a stupid question and still missed it. Starting to think this ability to miss the openly obvious might be a medical condition.

    Thank you for the help.

  5. #4
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: QTreeWidget child index

    Qt Code:
    1. QList<QTreeWidgetItem *> selected = ui->twg->selectedItems();
    2. QTreeWidgetItem *item = selected.first();
    3. QTreeWidgetItem *parent = item->parent();
    4. if(parent) {
    5. QTreeWidgetItem *tmp = new QTreeWidgetItem(parent);
    6. tmp->setText(0, "Dude");
    7. parent->addChild(tmp);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QList<QTreeWidgetItem *> selected = ui->twg->selectedItems();
    2. QTreeWidgetItem *item = selected.first();
    3. QTreeWidgetItem *parent = item->parent();
    4. if(parent) {
    5. int index = parent->indexOfChild(item);
    6. delete parent->takeChild(index);
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32
  2. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00
  3. Infinite loop - resize parent from child
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2006, 13:21
  4. Referencing Parent Widget from Child
    By taylor34 in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2006, 15:13
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32

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.