Results 1 to 4 of 4

Thread: QTreeWidget takeChildren() without removing

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget takeChildren() without removing

    Hi community,
    I am working with a QTreeWidget.

    I have a structure like this:

    A -> A1, A2, A3
    B -> B1, B2, B3
    C -> C1, C2, C3

    Where A, B and C are top-level nodes and the others are their respective children.
    I need to, after selecting any top-level node A, B or C, to select all their respective children
    For example:

    If I select A ( with the mouse ), then I need A1, A2 and A3 to be selected too.
    Looking at the documentation I saw the method QTreeWidgetItem::takeChildren(), but this will also remove the children. In my case I need them not to be removed, only selected.

    Any help please?

    Regards


    Added after 18 minutes:


    I need this behavior in my QTableWidget::startDrag method.
    When I select a top-level node, I need to drag and drop all its children
    Last edited by franco.amato; 20th January 2022 at 17:17.
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeWidget takeChildren() without removing

    Qt Code:
    1. // Pseudocode
    2.  
    3. QTreeWidgetItem * A = myTree->currentItem();
    4.  
    5. auto nChildren = A->childCount();
    6. for ( auto nChild = 0; nChild < nChildren; ++nChild )
    7. {
    8. QTreeWidgetItem * pChild = A->child( nChild );
    9. if ( pChild != nullptr )
    10. pChild->setSelected( true );
    11. }
    To copy to clipboard, switch view to plain text mode 

    I need this behavior in my QTableWidget::startDrag method.
    When I select a top-level node, I need to drag and drop all its children
    If you are moving items (especially from one tree widget to another) you will probably have to "take" the items at some point because a tree widget item cannot belong to two trees at the same time, nor can it live in two places in the same tree.
    Last edited by d_stranz; 20th January 2022 at 17:25.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    franco.amato (21st January 2022)

  4. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget takeChildren() without removing

    Thank you,
    it's possible to add those new selected items to the list returned by selectedItems ?

    Basically now I have implemented this code:

    Qt Code:
    1. void ReportsTreeWidget::startDrag(Qt::DropActions supportedActions)
    2. {
    3. Q_UNUSED(supportedActions)
    4.  
    5. if (selectedItems().empty())
    6. {
    7. return;
    8. }
    9.  
    10. // ... more code ...
    11.  
    12. // I iterate over the selected items
    13. for (QTreeWidgetItem *item : selectedItems())
    14. {
    15. // If top-level item then drag its children, not the item itself
    16. if (!item->parent())
    17. {
    18. auto children = item->childCount();
    19. for (auto child = 0; child < children; ++child)
    20. {
    21. QTreeWidgetItem *pChild = item->child(child);
    22. if (pChild != nullptr)
    23. {
    24. pChild->setSelected(true);
    25. }
    26. }
    27.  
    28. item->setSelected(false);
    29. }
    30.  
    31. // ... more code ...
    32.  
    33. QMimeData *mimeData = new QMimeData;
    34. mimeData->setText(data);
    35.  
    36. Drag drag(this);
    37. drag.setMimeData(mimeData);
    38. drag.setPixmap(dragPixmap);
    39. drag.exec(Qt::CopyAction);
    40. }
    To copy to clipboard, switch view to plain text mode 

    I compose "data" from the selected items that I get with the method selectedItems() in the for loop.
    I would add the new selected items (in the inner if) so that I can add them to the list. The items are not moved, but copied.
    Franco
    Last edited by franco.amato; 20th January 2022 at 18:58.
    Franco Amato

  5. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SOLVED] QTreeWidget takeChildren() without removing

    I solved the task by adding the new created item text to the data string.
    Franco Amato

Similar Threads

  1. removing items QTreeWidget
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2015, 22:58
  2. Replies: 0
    Last Post: 21st October 2013, 10:16
  3. Replies: 5
    Last Post: 8th March 2010, 18:51
  4. QTreeWidget - scrolls to top when removing items
    By durbrak in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2006, 23:02
  5. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 13:20

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.