PDA

View Full Version : Does QTableWidget::clear() delete pointers?



Slewman
26th February 2010, 14:12
Hey everyone!

I am working on a QTableWidget and there is alot of data i am adding and refreshing and i was wondering if when i call a QTableWidget::clear() if that will delete the pointers i have created and inserted to the table...

i dont want any memory leaks!

any help is greatly appreciated

Lykurg
26th February 2010, 14:22
Yes, as you can see in the sources:
void QTableModel::clear()
{
for (int j = 0; j < verticalHeaderItems.count(); ++j) {
if (verticalHeaderItems.at(j)) {
verticalHeaderItems.at(j)->view = 0;
delete verticalHeaderItems.at(j);; // <--
verticalHeaderItems[j] = 0;
}
}
for (int k = 0; k < horizontalHeaderItems.count(); ++k) {
if (horizontalHeaderItems.at(k)) {
horizontalHeaderItems.at(k)->view = 0;
delete horizontalHeaderItems.at(k);; // <--
horizontalHeaderItems[k] = 0;
}
}
clearContents();
}

void QTableModel::clearContents()
{
for (int i = 0; i < tableItems.count(); ++i) {
if (tableItems.at(i)) {
tableItems.at(i)->view = 0;
delete tableItems.at(i); // <--
tableItems[i] = 0;
}
}
reset();
}

Slewman
26th February 2010, 14:55
perfect thanks! oh and i got that icon sizing thing to work... fyi ;)

Lykurg
26th February 2010, 15:03
oh and i got that icon sizing thing to work... fyi ;)
Fine. Please add your solution to your thread that others can benefit from it.

trallallero
21st January 2011, 12:51
From my point of view, clear() has a bug.
It should not reset the Header but only all the rows of the list.
I've lost one day in trying to understand the problem and then it was just a call to clear() :(

Lykurg
21st January 2011, 16:09
Well, then you have to use QTableWidget::clearContents(). And header items are also items of the view, and the headers are connected to the contend I find it reasonable that clear clears both, header and content items.

Nevertheless, tough luck you lost a day!