PDA

View Full Version : QStandardItemModel with QStandardItem usage



jove04
24th April 2010, 02:57
I have a function that I call from my main form. I have a tableView on my main form.

First I create a model like this :
QStandardItemModel *model = new QStandardItemModel((frame->NM * frame->NLC), 6, this);

Then I populate it like this. This is done in a loop :
QStandardItem *stdd = new QStandardItem(QString::number(frame->member[i]->ResultantForce[ind]));
stdd->setTextAlignment(Qt::AlignHCenter);
model->setItem(m, n, stdd);

My question is when are the hundreds of the QStandardItem *stdd I created in a loop are deleted from memory? Do I have to delete them manually by using delete stdd or they are deleted automatically when the model is deleted? What about the model itself? When is it removed from memory.

Thank you in advance...

jove04
24th April 2010, 08:25
I would like to clarify my question:
1) Does model->clear() remove all the QStandardItem objects from memory? If not, then how do I remove them?
2) Do I have to remove the model itself manually or it will be removed automatically when the parent is destroyed?

The way memory is used and then reclaimed by these objects is not explained clearly in the manuals. I use QT Creator on windows 7.0 32 bit.

Thanks...

mnh1959
29th October 2010, 18:53
I would like to add an addtional question to this post...
When you add user defined data to a QStandardItem, is that cleaned up when model->clear() is called?
Some experiments that we are doing would indicate there is a memory leak. We update the model by clearing it, allocating new QStandardItems and add them to the model... The memory consumption increases over time.:(

ChrisW67
30th October 2010, 00:26
My question is when are the hundreds of the QStandardItem *stdd I created in a loop are deleted from memory? Do I have to delete them manually by using delete stdd or they are deleted automatically when the model is deleted? What about the model itself? When is it removed from memory.


From the docs for QStandardItemModel::setItem()


The model takes ownership of the item.

The items will definitely be destroyed when the model itself is destroyed. They probably will be destroyed earlier if you use clear() on the model but I have not verified that.

The model itself, in this situation, will definitely be destroyed when the the object "this" is destroyed unless you choose to do it yourself earlier. When you create a QObject with a parent, or set the parent directly or indirectly after creation, then the parent will delete it as part of parent's destruction. See the the article "Object Trees and Object Ownership" in Assistant and the detailed description section of the QObject docs.


Edit: Just did the experiment, clear() does trigger item delete


#include <QtGui>
#include <QDebug>

class MyItem: public QStandardItem
{
public:
MyItem(const QString & text): QStandardItem(text) {
qDebug() << "Item created" << this;
}
~MyItem() {
qDebug() << "Item destroyed" << this;
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item =
new MyItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}

qDebug() << "Finished making model";
model.clear();
qDebug() << "Model cleared";
}


Added after 5 minutes:

mnh1959: If the "user defined data" is a value type then sure. If it is a pointer then the space for the pointer is freed but object pointed to is not. You have to manage the target of the pointer yourself (in the destructor of a QStandardItem derivative).