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:
Code:
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?
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:
Code:
QWidget* widget
= tree
->itemWidget
(index,
0);
tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
tree->setItemWidget(newindex, 0, widget);
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?
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:
Code:
QWidget* widget
= tree
->itemWidget
(index,
0);
widget->setParent(this);
tree->insertTopLevelItem( newindex, tree->takeTopLevelItem( index ) );
tree->setItemWidget(newindex, 0, widget);
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.