Results 1 to 2 of 2

Thread: QFileSystemModel::remove not removing index from model

  1. #1

    Default QFileSystemModel::remove not removing index from model

    Hi,

    When I remove an index from QFileSystemModel, the file is being deleted and no longer exists but if I call QFileSystemModel::index with the path of the file I have just removed, it is still a valid index, but in the docs, it says that ::remove should remove the index from the model and that ::isValid on an index only returns true if the index has a model.

    I have tried to create a minimal example below:

    Qt Code:
    1. QString dirPath = "C:\\TestQFSM";
    2. QString file1Path = "C:\\TestQFSM\\file1";
    3. QString file2Path = "C:\\TestQFSM\\file2";
    4. QString file3Path = "C:\\TestQFSM\\file3";
    5.  
    6. QDir cDrive = QDir("C:\\");
    7. bool madeDirectory = cDrive.mkdir(dirPath);
    8. qDebug() << "Made directory? " << madeDirectory << " expecting true";
    9.  
    10. QFile file1(file1Path);
    11. file1.open( QIODevice::WriteOnly );
    12. QTextStream file1Stream(&file1);
    13. file1Stream << "Some text";
    14. file1.flush();
    15. file1.close();
    16. bool file1Exists = QFile(file1Path).exists();
    17. qDebug() << "File1Exists? " << file1Exists << " expecting true";
    18.  
    19. QFile file2(file2Path);
    20. file2.open( QIODevice::WriteOnly );
    21. QTextStream file2Stream(&file2);
    22. file2Stream << "Some text";
    23. file2.flush();
    24. file2.close();
    25. bool file2Exists = QFile(file2Path).exists();
    26. qDebug() << "File2Exists? " << file2Exists << " expecting true";
    27.  
    28. bool file3Exists = QFile(file3Path).exists();
    29. qDebug() << "File3Exists? " << file3Exists << " expecting false";
    30.  
    31. QFileSystemModel* fileSystemModel = new QFileSystemModel(this);
    32. fileSystemModel->setRootPath("");
    33.  
    34. bool file1Valid = fileSystemModel->index(file1Path).isValid();
    35. qDebug() << "File1 Valid? " << file1Valid << " expecting true";
    36.  
    37. bool file2Valid = fileSystemModel->index(file2Path).isValid();
    38. qDebug() << "File2 Valid? " << file2Valid << " expecting true";
    39.  
    40. bool file3Valid = fileSystemModel->index(file3Path).isValid();
    41. qDebug() << "File3 Valid? " << file3Valid << " expecting false";
    42.  
    43. bool file1Removed = fileSystemModel->remove(fileSystemModel->index(file1Path));
    44. qDebug() << "File1 Removed? " << file1Removed << " expecting true";
    45.  
    46. bool file2Removed = fileSystemModel->remove(fileSystemModel->index(file2Path));
    47. qDebug() << "File2 Removed? " << file2Removed << " expecting true";
    48.  
    49. bool file3Removed = fileSystemModel->remove(fileSystemModel->index(file3Path));
    50. qDebug() << "File3 Removed? " << file3Removed << " expecting false";
    51.  
    52. file1Exists = QFile(file1Path).exists();
    53. qDebug() << "File1Exists after removal? " << file1Exists << " expecting false";
    54.  
    55. file2Exists = QFile(file2Path).exists();
    56. qDebug() << "File2Exists after removal? " << file2Exists << " expecting false";
    57.  
    58. file3Exists = QFile(file3Path).exists();
    59. qDebug() << "File3Exists after removal? " << file3Exists << " expecting false";
    60.  
    61. file1Valid = fileSystemModel->index(file1Path).isValid();
    62. qDebug() << "File1 Valid after removal? " << file1Valid << " expecting false";
    63.  
    64. file2Valid = fileSystemModel->index(file2Path).isValid();
    65. qDebug() << "File2 Valid after removal? " << file2Valid << " expecting false";
    66.  
    67. file3Valid = fileSystemModel->index(file3Path).isValid();
    68. qDebug() << "File3 Valid after removal? " << file3Valid << " expecting false";
    To copy to clipboard, switch view to plain text mode 

    The output I am getting is:

    Made directory? true expecting true
    File1Exists? true expecting true
    File2Exists? true expecting true
    File3Exists? false expecting false
    File1 Valid? true expecting true
    File2 Valid? true expecting true
    File3 Valid? false expecting false
    File1 Removed? true expecting true
    File2 Removed? true expecting true
    QFileSystemWatcher::removePath: path is empty
    QFile::remove: Empty or null file name
    File3 Removed? false expecting false
    File1Exists after removal? false expecting false
    File2Exists after removal? false expecting false
    File3Exists after removal? false expecting false
    File1 Valid after removal? true expecting false
    File2 Valid after removal? true expecting false

    File3 Valid after removal? false expecting false

    So all the values are as expected apart from the ones marked with red. Can someone explain why the indices are still valid?

    Thanks for your time,

    Matt Grover

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFileSystemModel::remove not removing index from model

    It seems to me QFileSystemModel doesn't remove the file entry from its cache when remove() is called and then creates the index successfully because the entry exists in the cache although the file itself can't be found (the comment in code says in this case:
    Qt Code:
    1. // we couldn't find the path element, we create a new node since we
    2. // _know_ that the path is valid
    To copy to clipboard, switch view to plain text mode 

    I'd call it a bug and an incorrect assumption
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Proxy model, index mapping.
    By wojtekw in forum Qt Programming
    Replies: 14
    Last Post: 7th January 2016, 08:09
  2. Replies: 0
    Last Post: 7th October 2010, 19:38
  3. QDataWidgetMapper setting own model index
    By AlGaN in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2010, 16:34
  4. Model/View one index.column as hidden UserRole
    By doitux in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2008, 12:08
  5. removing model Items
    By gyre in forum Newbie
    Replies: 2
    Last Post: 25th November 2007, 20:10

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.