Results 1 to 4 of 4

Thread: QListWidget - clear()

  1. #1
    Join Date
    May 2009
    Posts
    52
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget - clear()

    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
    Qt Code:
    1. void BlaBla::setPath(const QString& path)
    2. {
    3. QFileIconProvider iconProvider;
    4.  
    5. m_path = path;
    6. m_dir = QDir(path);
    7.  
    8. clear();
    9. if (path == "My Computer")
    10. {
    11. for (int i = 0; i < m_dir.drives().size(); ++i)
    12. {
    13. QFileInfo info = m_dir.drives().at(i);
    14.  
    15. QString name;
    16. name.append(Globals::getVolumeInfo(info.filePath()).volumeName);
    17. name.append(" (");
    18. name.append(info.filePath().left(2));
    19. name.append(")");
    20. QListWidgetItem* item = new QListWidgetItem(iconProvider.icon(info), name, this);
    21. item->setData(Qt::UserRole + 1, info.filePath());
    22. }
    23. }
    24. else
    25. {
    26. QFileInfo qInfo;
    27.  
    28. QString tempPath = m_dir.path() + QDir::separator();
    29.  
    30. if (m_dir.path() == QDir::home().absolutePath().append("/Desktop"))
    31. {
    32. QListWidgetItem* itemMyComputer = new QListWidgetItem(iconProvider.icon(QFileIconProvider::Computer), "My Computer", this);
    33. itemMyComputer->setData(Qt::UserRole + 1, "My Computer");
    34.  
    35. QListWidgetItem* itemMyDocuments = new QListWidgetItem(iconProvider.icon(QFileIconProvider::Folder), "My Documents", this);
    36. itemMyDocuments->setData(Qt::UserRole + 1, "My Documents");
    37. }
    38.  
    39. QList<FileInfo> theList = Globals::getFolderContens(m_dir.path().replace("/", "\\").append("\\*"), false);
    40. QList<FileInfo>::const_iterator i;
    41.  
    42. for (i = theList.begin(); i != theList.end(); ++i)
    43. {
    44. if ((*i).isDir)
    45. {
    46. QListWidgetItem* item = new QListWidgetItem(iconProvider.icon(QFileIconProvider::Folder), (*i).name, this);
    47. item->setData(Qt::UserRole + 1, tempPath + (*i).name);
    48. }
    49. else
    50. {
    51. qInfo.setFile(m_dir.path() + QDir::separator() + (*i).name);
    52. QListWidgetItem* item = new QListWidgetItem(iconProvider.icon(qInfo), (*i).name, this);
    53. item->setData(Qt::UserRole + 1, tempPath + (*i).name);
    54. }
    55. }
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 

    Am I doing something wrong?

    I'm using Qt 4.5

  2. #2
    Join Date
    Oct 2009
    Posts
    28
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget - clear()

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QListWidget - clear()

    Quote Originally Posted by gutiory View Post
    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:
    Qt Code:
    1. void QListWidget::clear()
    2. {
    3. selectionModel()->clear();
    4. d->listModel()->clear();
    5. }
    6. //...
    7. void QListModel::clear()
    8. {
    9. for (int i = 0; i < items.count(); ++i) {
    10. if (items.at(i)) {
    11. items.at(i)->d->theid = -1;
    12. items.at(i)->view = 0;
    13. delete items.at(i);
    14. }
    15. }
    16. items.clear();
    17. reset();
    18. }
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    May 2009
    Posts
    52
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget - clear()

    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

Similar Threads

  1. Clear a cache!
    By Indalo in forum Qwt
    Replies: 1
    Last Post: 5th March 2010, 16:59
  2. how can i clear a file?
    By Vincenzo in forum Newbie
    Replies: 2
    Last Post: 18th January 2009, 11:05
  3. Qt clear fonts
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 2nd July 2008, 07:45
  4. QTreeWidget->clear()?
    By vishal.chauhan in forum Qt Programming
    Replies: 20
    Last Post: 5th October 2007, 07:00
  5. How to Clear QTreeWidget
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2007, 07:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.