PDA

View Full Version : QListWidget - clear()



Carlsberg
2nd June 2010, 12:22
Hello,

I have a QListWidget subclass which I use as a file browser. The list is filled using the setPath() function. My problem is that going through folders using the list shows an ever growing memory consumption in Windows Task Manager. Now I am using clear() at the beginning of the function and I expect that clear() does what it says in the docs: deletes all items in the list


Removes all items and selections in the view.

Note: All items will be permanently deleted.

This is the code

void BlaBla::setPath(const QString& path)
{
QFileIconProvider iconProvider;

m_path = path;
m_dir = QDir(path);

clear();
if (path == "My Computer")
{
for (int i = 0; i < m_dir.drives().size(); ++i)
{
QFileInfo info = m_dir.drives().at(i);

QString name;
name.append(Globals::getVolumeInfo(info.filePath() ).volumeName);
name.append(" (");
name.append(info.filePath().left(2));
name.append(")");
QListWidgetItem* item = new QListWidgetItem(iconProvider.icon(info), name, this);
item->setData(Qt::UserRole + 1, info.filePath());
}
}
else
{
QFileInfo qInfo;

QString tempPath = m_dir.path() + QDir::separator();

if (m_dir.path() == QDir::home().absolutePath().append("/Desktop"))
{
QListWidgetItem* itemMyComputer = new QListWidgetItem(iconProvider.icon(QFileIconProvide r::Computer), "My Computer", this);
itemMyComputer->setData(Qt::UserRole + 1, "My Computer");

QListWidgetItem* itemMyDocuments = new QListWidgetItem(iconProvider.icon(QFileIconProvide r::Folder), "My Documents", this);
itemMyDocuments->setData(Qt::UserRole + 1, "My Documents");
}

QList<FileInfo> theList = Globals::getFolderContens(m_dir.path().replace("/", "\\").append("\\*"), false);
QList<FileInfo>::const_iterator i;

for (i = theList.begin(); i != theList.end(); ++i)
{
if ((*i).isDir)
{
QListWidgetItem* item = new QListWidgetItem(iconProvider.icon(QFileIconProvide r::Folder), (*i).name, this);
item->setData(Qt::UserRole + 1, tempPath + (*i).name);
}
else
{
qInfo.setFile(m_dir.path() + QDir::separator() + (*i).name);
QListWidgetItem* item = new QListWidgetItem(iconProvider.icon(qInfo), (*i).name, this);
item->setData(Qt::UserRole + 1, tempPath + (*i).name);
}
}
}
}

Am I doing something wrong?

I'm using Qt 4.5

gutiory
2nd June 2010, 13:22
Hello.
I'm not completly sure, but I think you must delete each item by yourself. I mean, you must go through every item (you may use an iterator) and deleting each one.
You can make a test and look the memory consumption at the Windows Task Manager.

Regards.

Lykurg
2nd June 2010, 14:47
I'm not completly sure, but I think you must delete each item by yourself.
No you don't have to. One nice thing of Qt is, that it is open source:
void QListWidget::clear()
{
Q_D(QListWidget);
selectionModel()->clear();
d->listModel()->clear();
}
//...
void QListModel::clear()
{
for (int i = 0; i < items.count(); ++i) {
if (items.at(i)) {
items.at(i)->d->theid = -1;
items.at(i)->view = 0;
delete items.at(i);
}
}
items.clear();
reset();
}

and the Windows Task Manager is not an appropriate tool to measure memory. The memory is given free but still reserved for your application I guess.

Carlsberg
3rd June 2010, 07:01
Yes, you're right. Windows Task Manager is showing crap with a bad label. If I minimize the window and get it back again, the memory "used" is gone