PDA

View Full Version : QTreeWidget::setItemWidget() - item disappears after moving item



ultr
8th April 2011, 18:50
I'm inserting a QWidget into the item with QTreeWidget::setItemWidget().

But after moving the item with the code below, the widget disappears:
tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
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?

ChiliPalmer
8th April 2011, 20:47
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:


QWidget* widget = tree->itemWidget(index, 0);
tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
tree->setItemWidget(newindex, 0, widget);

ultr
8th April 2011, 21:22
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?

ChiliPalmer
8th April 2011, 21:54
No, not really. The only thing I can think of is to take ownership of the widget, too, e.g:


QWidget* widget = tree->itemWidget(index, 0);
widget->setParent(this);
tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
tree->setItemWidget(newindex, 0, widget);

ultr
8th April 2011, 21:59
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.