Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileSystemModel/QTreeView, rename failed after expanded tree item



  • Hello,

    I have an error when use QFileSystemModel/QTreeView together, If there is FolderA/FolderB,

    1. expanded FolderA

    2. expanded FolderB

    3. rename FolderA, failed!

    the same code on mac, play very well, will rename successfully! But on windows, rename failed!

    It confused me for several days , can anybody help me?

    There is a sample project below.
    sample code -- look at here->



  • If someone has a way to forbid this problem, please tell me ! THANK YOU VERY MUCH!



  • @Lucky-Juan
    I have looked at your QT bug report and the screenshot there. I wrote

    Looking very carefully at your error message box, it seems to me the message shows "FolderA ", i.e. a space at the end? If so that is not a legal Windows file/directory name, hence the message?

    Is that it, and nothing to do with the situation you report??



  • This post is deleted!


  • @JonB said in QFileSystemModel/QTreeView, rename failed after expanded tree item:

    @Lucky-Juan
    I have looked at your QT bug report and the screenshot there. I wrote

    Looking very carefully at your error message box, it seems to me the message shows "FolderA ", i.e. a space at the end? If so that is not a legal Windows file/directory name, hence the message?

    Is that it, and nothing to do with the situation you report??

    I added another two pictures, it is not "a space" problem, no space will also cause this problem.



  • @Lucky-Juan
    OK, then whatever your problem is here I think it (the error message at least) will come under https://bugreports.qt.io/browse/QTBUG-13386 from 2010!

    Change the rename failed message in QFileSystemModel::setData() to be more meaningful as it might not be the name itself that is causing the rename to fail, it could be because the file is open or something

    The folder you are trying to rename is "locked", and the error message is misleading.



  • @JonB
    Thank you for your reply!
    Yeah, it seems that the same reason which the file is "locked". But I just expanded the treeview, and do nothing else, then, it will rename failed. And, the same code on MAC, it can rename successfully. So, it is not a QT bug?
    And if the file is locked, is there any way to unlock it?



  • @Lucky-Juan
    I don't know what you can do about it (wait for further news on the Qt-BUG you reported?). But it may not be a Qt "bug", it may be to do with the way Windows handles the "locking" of parent directories which is different from Mac/Linux, giving rise to this problem.

    I did have a go in Windows Explorer at renaming (in its left-hand pane) a parent directory of the directory it is currently showing (in its right-hand pane), which is as close as I can get to your case. It did allow the rename.

    OTOH, If you open a Command Prompt and cd to FolderA\FolderB, you then get an error in File Explorer if you try to rename (either of) these directories. Whereas I have a feeling that would work OK from Linux. So you can see some potential issues/differences....

    The Win API calls to rename a directory are the MoveFile() (https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefile) set of similarly-named functions. I would try renaming your directory from code with one of these and see whether it succeeds or not. If it does it tells you the issue lies in Qt's rename; if does not it tells you the issue lies in how Qt code is holding the parent directory open.



  • @JonB
    I try to debug into QT code, found that rename will call the MoveFile() of Win API which you said (look at the picture ), and it returns false. The return errorcode is 5, and I found it means "Access is denied."
    I guess that the issue lies in QFileSystemModel is holding the dir handle.
    debug into qt-pic -- Screenshot_7



  • @Lucky-Juan
    I imagine this is indeed the cause. I do not know why, but presumably/perhaps there is a good reason for a QTreeView to hold an open handle onto a directory which is an ancestor of the one shown, or something like that.

    IIRC, Linux (MacOS) tends to be OK about, say, renaming a file with an open handle on it, whereas Windows tends to refuse with a "locked/access denied".

    I have a feeling that your best hope for a response is if a dev looks into your report on the Qt Bug board. It will take someone who is prepared to delve into the QFileSystemModel from QTreeView code, or since you have looked at some of that code yourself you might choose to look a little further. This is an open-source/user community, sometimes to get what you want it is you who has to do the work!

    If not --- and there is a danger your issue will go unresolved --- you may have to either accept the situation (is it that bad for your user that this situation fails under Windows?), or perhaps something hideous like intercept the rename event, close/disconnect the tree or model, do the rename, and then re-open/connect and restore to where you were. Probably not nice, but if you need the facility....


Log in to reply