Results 1 to 3 of 3

Thread: Moving QTreeWidgetItem Up and Down in a QTreeWidget

  1. #1
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Question Moving QTreeWidgetItem Up and Down in a QTreeWidget

    Hi,

    I want to be able to swap two QTreeWidgetItem positions in a QtreeWidget.
    With other words move items up and down 1 position in a QtreeWidget.

    Move.fw.png

    After searching for hours online I just can't find a proper solution.
    Not a single one of them works.
    Have to add to this that I'm very new to QT.

    Does anyone have a solution to my problem?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Moving QTreeWidgetItem Up and Down in a QTreeWidget

    You get the parent item, then you call its indexOfChild method to determine the index if your current item, then call takeItem() and then insertItem with either index - 1 or index + 1

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Moving QTreeWidgetItem Up and Down in a QTreeWidget

    Worked as a charm!
    Thank you very much.

    Qt Code:
    1. QTreeWidgetItem* item = this->currentItem();
    2. int row = this->currentIndex().row();
    3. if (item && row > 0)
    4. {
    5. QTreeWidgetItem* parent = item->parent();
    6. int index = parent->indexOfChild(item);
    7. QTreeWidgetItem* child = parent->takeChild(index);
    8. parent->insertChild(index-1, child);
    9. parent->setExpanded(true);
    10. child->setExpanded(true);
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Moving items in a QTreeWidget
    By sudheer168 in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2011, 16:01
  2. QTreeWidget::setCurrentItem ( QTreeWidgetItem * item )
    By gboelter in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2011, 23:21
  3. Customizing QTreeWidget and QTreeWidgetItem
    By mots_g in forum Qt Programming
    Replies: 0
    Last Post: 18th August 2010, 07: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, 07:52
  5. moving QTreeWidgetItem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2009, 21:17

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.