Does QTableWidget::setItem() automatically delete the previous object when it is called for the second time?
Solved
General and Desktop
-
wrote on 18 Jul 2022, 08:03 last edited by
When I read setCellWidget() function, the doc says If cell widget A is replaced with cell widget B, cell widget A will be deleted. and gives out a code snippet example:
setCellWidget(row, column, new QLineEdit); ... //Previously created QLineEdit object will be deleted. setCellWidget(row, column, new QTextEdit);
Now I am wondering does setItem() works like setCellWidget() ? For example:
setItem(row, column, new QTableWidgetItem); ... //Do I need to manually delete the previous QTableWidgetItem object, or free up the memory occupied by the pointer in other words? setItem(row, column, new QTableWidgetItem);
-
When I read setCellWidget() function, the doc says If cell widget A is replaced with cell widget B, cell widget A will be deleted. and gives out a code snippet example:
setCellWidget(row, column, new QLineEdit); ... //Previously created QLineEdit object will be deleted. setCellWidget(row, column, new QTextEdit);
Now I am wondering does setItem() works like setCellWidget() ? For example:
setItem(row, column, new QTableWidgetItem); ... //Do I need to manually delete the previous QTableWidgetItem object, or free up the memory occupied by the pointer in other words? setItem(row, column, new QTableWidgetItem);
wrote on 18 Jul 2022, 08:09 last edited by@Zhmou
Because https://doc.qt.io/qt-6/qtablewidget.html#setItem says:The table takes ownership of the item.
that means it owns it, and will dispose it if you replace it.
1/2