PDA

View Full Version : Editing a QTableWidgetItem and memory leaks



AndresBarbaRoja
28th June 2011, 17:41
Good day,
I'm trying to edit some QTableWidgetItem values and currently this is what I'm doing


QTableWidgetItem *temp = new QTableWidgetItem("String 1");
QTableWidgetItem *power = new QTableWidgetItem("String 2");
QTableWidgetItem *dutyCycle = new QTableWidgetItem("String 3");
QString mnemo = "String";

for(int i=0;i<29;i++)
{
QTableWidgetItem * head = ui->tLines_tableWidget->verticalHeaderItem(i);
if (head->text()==mnemo)
{
ui->tLines_tableWidget->setItem(i,0,temp);
ui->tLines_tableWidget->setItem(i,2,dutyCycle);
ui->tLines_tableWidget->setItem(i,3,power);
break;
}
}


My question is, what happens with the item that i'm replacing? I create the new items with "new" but i never delete them, so do i have a memory leak here?
If i'm losing memory here, how coud i avoid that?

Thanks in advance,
A.G.

DanH
28th June 2011, 18:09
http://doc.qt.nokia.com/4.7/qtablewidget.html#setItem

Note that "The table takes ownership of the item." This means that the table is responsible for deleting the item at the appropriate time.

If, for some reason, you want to get the old item back and manage it yourself use "takeItem".