Reputation: 106
My source code ↓
ui->tableWidget->setItem(0,7,new QTableWidgetItem(QString::number(3)));
ui->tableWidget->item(0,7)->setTextAlignment(Qt::AlignCenter);
My approach :
delete ui->tableWidget->item(0,7);
If this memory is free?
If not, let me know any other method.
Upvotes: 1
Views: 6167
Reputation: 16866
The call to setItem(...)
passes ownership of the QTableWidgetItem
to the QTableWidget
.
Although QTableWidgetItem
is not a QObject
, it does take care to inform the QTableWidget
about its deletion (from qtablewidget.cpp, Qt 5.1.1):
QTableWidgetItem::~QTableWidgetItem()
{
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
model->removeItem(this);
view = 0;
delete d;
}
takeItem()
sets the view of the item to null
, releasing the ownership to the caller.
Because of this, the above code in the item's destructor model->removeItem(this);
will not be called.
This means that you need to manually delete the QTableWidgetItem
.
But it doesn't matter if you call takeItem(...)
or not before deleting the item.
See also Remove QListWidgetItem: QListWidget::takeItem(item) vs delete item.
Upvotes: 4