Results 1 to 5 of 5

Thread: QTreeWidget::setItemWidget() - item disappears after moving item

  1. #1
    Join Date
    Nov 2006
    Location
    Poland
    Posts
    35
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Exclamation QTreeWidget::setItemWidget() - item disappears after moving item

    I'm inserting a QWidget into the item with QTreeWidget::setItemWidget().

    But after moving the item with the code below, the widget disappears:
    Qt Code:
    1. tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
    To copy to clipboard, switch view to plain text mode 
    Calling setItemWidget() again does not help.
    What is more, application crashes when trying to access the widget which was previously inserted to that item.
    Documentation says nothing about these issues.

    How do I move items in QTreeWidget while keeping their inserted widgets?

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget::setItemWidget() - item disappears after moving item

    The Doc says the tree takes ownership of the widget, not the item.
    So did you try taking the widget before moving the item? Something like:
    Qt Code:
    1. QWidget* widget = tree->itemWidget(index, 0);
    2. tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
    3. tree->setItemWidget(newindex, 0, widget);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2006
    Location
    Poland
    Posts
    35
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTreeWidget::setItemWidget() - item disappears after moving item

    Unfortunately this doesn't work either.

    I guess QTreeWidget deletes the widget when QTreeWidget::takeTopLevelItem() is called.
    Is there a way of changing item index without removing the item from the tree?

  4. #4
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget::setItemWidget() - item disappears after moving item

    No, not really. The only thing I can think of is to take ownership of the widget, too, e.g:

    Qt Code:
    1. QWidget* widget = tree->itemWidget(index, 0);
    2. widget->setParent(this);
    3. tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
    4. tree->setItemWidget(newindex, 0, widget);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2006
    Location
    Poland
    Posts
    35
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTreeWidget::setItemWidget() - item disappears after moving item

    I have tried this as well. Doesn't work. Probably QTreeWidget tracks widgets by itself and widget's parent doesn't matter.

    I have solved this by creating a new widget and copying all the data from the previous one.

    Still I think QTreeWidget should provide a method of moving items.


    PS. This problem also occurs for items' drag&drop.

Similar Threads

  1. Replies: 6
    Last Post: 7th April 2011, 14:05
  2. Replies: 2
    Last Post: 3rd February 2011, 11:07
  3. Replies: 7
    Last Post: 19th October 2010, 09:32
  4. Replies: 2
    Last Post: 8th October 2010, 18:12
  5. moving one graphical item over other item...
    By salmanmanekia in forum Qt Programming
    Replies: 36
    Last Post: 3rd July 2008, 08:59

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.