Results 1 to 7 of 7

Thread: QFileSystemModel remove bug

  1. #1
    Join Date
    May 2013
    Posts
    321
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Exclamation QFileSystemModel remove bug

    Hi,
    I have a qfilesystemmodel and I have a menu to delete a file/folder.
    Here the code :
    Qt Code:
    1. for( int i = 0; i < SelectedIndexes.count(); ++i )
    2. {
    3. const QFileInfo FileInfo = Model->fileInfo( SelectedIndexes[ i ] );
    4. if( FileInfo.isDir() )
    5. {
    6. if( QDir( FileInfo.absoluteFilePath() ).removeRecursively() == false )
    7. DE::CEngine::GetLogger().LogError( "\"%s\" can not be deleted", FileInfo.absoluteFilePath().toUtf8().data() );
    8. }
    9. else
    10. {
    11. if( QDir( FileInfo.dir().absolutePath() ).remove( FileInfo.fileName() ) == false )
    12. DE::CEngine::GetLogger().LogError( "\"%s\" can not be deleted", FileInfo.absoluteFilePath().toUtf8().data() );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    The problem is when you enter the folder inside a folder and try to remove, you can't remove.
    * Root
    ** Folder
    *** Inside_Folder
    Once a setRootIndex is called on the "Inside_Folder" if you set the rootIndex to the root and try to remove Folder, you can't.
    That removes "Inside_Folder" but not "Folder" you have to redo a remove action on Folder a second time to remove it.
    A fix exists on this problem ? How ?
    Thanks for the help

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QFileSystemModel remove bug

    Maybe it is a bug, but you can always check to see if the file / folder exists after you do a remove, and have your code do it again. You don't have to make your user do it twice when you can do it for them.

  3. #3
    Join Date
    May 2013
    Posts
    321
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: QFileSystemModel remove bug

    Doing it twice if the remove return false gives two times the error message and I can't remove the folder outside the app using Windows explorer, Windows says the folder is used can't be deleted. Looks like a QFileWatcher problem. I tried to remove recursively manually but gives the same result, that remove all inside recursively but not the folder where the delete starts.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QFileSystemModel remove bug

    I think the problem might be that because the QFileInfo instance is still in scope and is still "looking" at the folder, that is why you are getting the "in use" message.

    Since you don't actually need the QFileInfo instance (you need the absolute path, filename and a bool to say if it is a directory), then you could put the QFileInfo instance into its own local scope:

    Qt Code:
    1. for( int i = 0; i < SelectedIndexes.count(); ++i )
    2. {
    3. QString absPath;
    4. QString fileName;
    5. bool isDir = false;
    6. {
    7. const QFileInfo FileInfo = Model->fileInfo( SelectedIndexes[ i ] );
    8. absPath = FileInfo.absoluteFilePath();
    9. isDir = FileInfo.isDir();
    10. if ( isDir )
    11. fileName = FileInfo.fileName();
    12. }
    13.  
    14. if ( isDir )
    15. {
    16. if( QDir( absPath ).removeRecursively() == false )
    17. DE::CEngine::GetLogger().LogError( "\"%s\" can not be deleted", absPath.toUtf8().data() );
    18. }
    19. else
    20. {
    21. if( QDir( absPath ).remove( fileName ) == false )
    22. DE::CEngine::GetLogger().LogError( "\"%s\" can not be deleted", absPath.toUtf8().data() );
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2013
    Posts
    321
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: QFileSystemModel remove bug

    Same result, needs double action.
    I found the code can be reduced like that :
    Qt Code:
    1. for( int i = 0; i < SelectedIndexes.count(); ++i )
    2. {
    3. if( Model->remove( SelectedIndexes[ i ] ) == false )
    4. {
    5. const QString AbsoluteFilePath = Model->fileInfo( SelectedIndexes[ i ] ).absoluteFilePath();
    6. DE::CEngine::GetLogger().LogError( "\"%s\" can not be deleted", AbsoluteFilePath.toUtf8().data() );
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    The problem is there only if I enter in the folder before remove it, I delete it on the start of the application, that works fine.
    Root
    **Folder
    ***Inside_Folder
    Only if you enter the "Inside_Folder", if you just enter "Folder" and go back to "Root" and delete "Folder" that delete "Folder" and "Inside_Folder" correctly.
    The Qt doc says that installs a file system watcher : http://doc.qt.io/qt-5/qfilesystemmodel.html#setRootPath
    I'm pretty sure that doesn't remove it when you change the path, must be the problem, I only see that.
    When I change folder I do : m_DetailList->setRootIndex( DetailModel->index( CurrentPath ) );
    That shows me the inside of the folder since I use an icon view on the list.
    Last edited by Alundra; 1st March 2015 at 04:57.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QFileSystemModel remove bug

    If you are calling setRootIndex() then it possibly invalidates any existing QModelIndexes, including those in your SelectionIndexes vector/list. Are you rebuilding this list after your change of root index? Have you checked the indexes are valid and pointing where you think they are?

    Is there a reason for not deleting through the model?

  7. #7
    Join Date
    May 2013
    Posts
    321
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: QFileSystemModel remove bug

    Quote Originally Posted by ChrisW67 View Post
    Is there a reason for not deleting through the model?
    I have changed to use "Model->remove( SelectedIndexes[ i ] )" like I showed before.

    "setRootIndex()" is called only when double on a folder, when delete is involved, "setRootIndex()" is not called.
    On the beginning of the delete function I do :
    const QModelIndexList SelectedIndexes = selectedIndexes();
    I use icon view on the list, the user can not select inside folder since he only see the root folder of the delete.
    Since that deletes all folder inside, the index is valid. Only if I enter in the inside folder then it's impossible to delete without two delete actions.
    The full function :
    Qt Code:
    1. void DeleteSelectedItems()
    2. {
    3. const QModelIndexList SelectedIndexes = selectedIndexes();
    4. if( selectedIndexes().empty() )
    5. return;
    6. CFileSystemModel* Model = static_cast< CFileSystemModel* >( model() );
    7. for( int i = 0; i < SelectedIndexes.count(); ++i )
    8. {
    9. if( Model->remove( SelectedIndexes[ i ] ) == false )
    10. {
    11. const QString AbsoluteFilePath = Model->fileInfo( SelectedIndexes[ i ] ).absoluteFilePath();
    12. DE::CEngine::GetLogger().LogError( "\"%s\" can not be deleted", AbsoluteFilePath.toUtf8().data() );
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Alundra; 1st March 2015 at 15:27.

Similar Threads

  1. QFileSystemModel and QML
    By enrico5th in forum Qt Quick
    Replies: 5
    Last Post: 18th June 2014, 16:23
  2. Replies: 0
    Last Post: 9th November 2013, 03:25
  3. QFileSystemModel::remove not removing index from model
    By revorgm in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 10:24
  4. Help with QFileSystemModel
    By TheShow in forum Qt Programming
    Replies: 4
    Last Post: 5th January 2010, 21:11
  5. QDirModel or QFileSystemModel?
    By ricardo in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2009, 18: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
  •  
Qt is a trademark of The Qt Company.