Removing and deleting QTreeWidgetItem from QWidget within it.
-
Hi,
Having read all the available documentation, I cannot conclude if removing (taking) and deleting a QTreeWidgetItem from within one of it's children QWidget, is safe. Attached is a sample of what we have right now.

Anything we have tried till now, always start as a signal from QPushButton and ends up removing and deleting the QTreeWidget (and so the QWidget that emitted the signal..). And, because we are in the GUI thread, the connection is direct, so the chain starting from the signal of the button till the deletion, is like calling a function. But is something like that safe? Cause, with direct connection, we end up deleting also the object that emitted the signal, before the function returns.. Also there is no deleteLater() in QTreeWidgetItem.
No crash was witnessed till now also, but this is not something we can base our development on.
Is the only solution, to make the connection queued, so the QTreeWidgeItem and it's QWidgets are removed, in a later phase of Qt Even Loop?Thank you in advance.
-
Hi,
Having read all the available documentation, I cannot conclude if removing (taking) and deleting a QTreeWidgetItem from within one of it's children QWidget, is safe. Attached is a sample of what we have right now.

Anything we have tried till now, always start as a signal from QPushButton and ends up removing and deleting the QTreeWidget (and so the QWidget that emitted the signal..). And, because we are in the GUI thread, the connection is direct, so the chain starting from the signal of the button till the deletion, is like calling a function. But is something like that safe? Cause, with direct connection, we end up deleting also the object that emitted the signal, before the function returns.. Also there is no deleteLater() in QTreeWidgetItem.
No crash was witnessed till now also, but this is not something we can base our development on.
Is the only solution, to make the connection queued, so the QTreeWidgeItem and it's QWidgets are removed, in a later phase of Qt Even Loop?Thank you in advance.
@QJohn Use https://doc.qt.io/qt-6/qobject.html#deleteLater instead of directly deleting widgets.
-
Thank you for your reply.
Unfortunately, there is no deleteLater in QTreeWidgetItem.. Does the QTreeWidgetItem calls deleteLater for all the QWidget it holds?Maybe the right way is to remove first the widget from QTreeWidgetItem, then schedule it for deletion with deleteLater and then delete the QTreeWidgetItem?
-
Thank you for your reply.
Unfortunately, there is no deleteLater in QTreeWidgetItem.. Does the QTreeWidgetItem calls deleteLater for all the QWidget it holds?Maybe the right way is to remove first the widget from QTreeWidgetItem, then schedule it for deletion with deleteLater and then delete the QTreeWidgetItem?
@QJohn said in Removing and deleting QTreeWidgetItem from QWidget within it.:
Does the QTreeWidgetItem calls deleteLater for all the QWidget it holds?
I do not have a complete answer for the best/safest way to do what you are asking. But for this question it calls void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget) (with
widget == nullptr) and that callsoldWidget->deleteLater();.I am still unsure about the guarantees or lack of them if deleting a widget inside a signal from that widget, if direct connection is used. Though presumably
deleteLater()(but not necessarilydelete) should be OK. -
May be you can try
void btn_clicked(){ btn->setParent(nullptr); deleteItem(); btn->deleteLater(); }But my opinion if you call directly
void btn_clicked(){ deleteItem(); }It will work.
QTreeWidgetItem::~QTreeWidgetItem()
Destroys this tree widget item.
The item will be removed from QTreeWidgets to which it has been added. This makes it safe to delete an item at any time.