Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileSystemModel/QTreeView, rename failed after expanded tree item
QtWS25 Last Chance

QFileSystemModel/QTreeView, rename failed after expanded tree item

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucky.Juan
    wrote on last edited by Lucky.Juan
    #1

    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->

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Lucky.Juan
      wrote on last edited by
      #2

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

      JonBJ 1 Reply Last reply
      0
      • L Lucky.Juan

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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??

        L 1 Reply Last reply
        0
        • L Offline
          L Offline
          Lucky.Juan
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • JonBJ JonB

            @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??

            L Offline
            L Offline
            Lucky.Juan
            wrote on last edited by
            #5

            @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.

            JonBJ 1 Reply Last reply
            1
            • L Lucky.Juan

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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.

              L 1 Reply Last reply
              1
              • JonBJ JonB

                @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.

                L Offline
                L Offline
                Lucky.Juan
                wrote on last edited by
                #7

                @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?

                JonBJ 1 Reply Last reply
                0
                • L Lucky.Juan

                  @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?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @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.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Lucky.Juan
                    wrote on last edited by Lucky.Juan
                    #9

                    @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

                    JonBJ 1 Reply Last reply
                    0
                    • L Lucky.Juan

                      @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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @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....

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved