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. Cannot rename by QFile::rename
Qt 6.11 is out! See what's new in the release blog

Cannot rename by QFile::rename

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 7 Posters 10.9k Views 2 Watching
  • 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.
  • thamT tham

    Try it on mac os, it works perfectly, haven't tried on linux yet, I guess this maybe some sort of bug

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #6

    @tham I don't think it's a bug. It sounds more like the file is still in use. Windows blocks renaming/deleting of used/opened files, but MacOS/Linux do not. You should check using https://technet.microsoft.com/en-us/sysinternals/filemon.aspx

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    thamT 1 Reply Last reply
    6
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #7

      Also to add to @jsulm , I've had situations on windows, especially win8 and 10 where the user has windows explorer open in the same directory with the preview pane active. That might lock the file also.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      6
      • thamT Offline
        thamT Offline
        tham
        wrote on last edited by
        #8

        I find out the culprit, it is QImageReader.

        static size_t num = 0;
        QImageReader reader(name);
         reader.setDecideFormatFromContent(true);
         reader.canRead(); //after calling can read, the file will be locked
         bool const can_rename = QFile::rename(name, QString("img/sos%1").arg(num++));
        qDebug()<<"can rename:"<<can_rename;
        

        Is this an expected behavior?

        In case it is a bug, I create a small project to reproduce the issues

        I create a minimal project to reproduce the situation, place at mega.

        Things to noticed :

        1 : copy img_links.txt to your shadow build folder

        shadow_build
        --debug
        --release
        --img_links.txt

        2 : Create a folder "img" in shadow build folder

        shadow_build
        --debug
        --release
        --img

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @tham I don't think it's a bug. It sounds more like the file is still in use. Windows blocks renaming/deleting of used/opened files, but MacOS/Linux do not. You should check using https://technet.microsoft.com/en-us/sysinternals/filemon.aspx

          thamT Offline
          thamT Offline
          tham
          wrote on last edited by
          #9

          @jsulm said in Cannot rename by QFile::rename:

          I don't think it's a bug. It sounds more like the file is still in use. Windows blocks renaming/deleting of used/opened files, but MacOS/Linux do not.

          Thanks, learn new things. Looks like QImageReader blocking the file.

          jsulmJ 1 Reply Last reply
          0
          • thamT tham

            @jsulm said in Cannot rename by QFile::rename:

            I don't think it's a bug. It sounds more like the file is still in use. Windows blocks renaming/deleting of used/opened files, but MacOS/Linux do not.

            Thanks, learn new things. Looks like QImageReader blocking the file.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @tham That could be actually a bug in QImageReader.
            canRead() should just check whether the image can be read and close it if it was opened to check.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #11

              The internals are a bit more complex than that.

              I'd move the QImageReader code in its own scope so it gets destroyed when not needed anymore and then you can go on move your files around.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              thamT E 2 Replies Last reply
              3
              • SGaistS SGaist

                The internals are a bit more complex than that.

                I'd move the QImageReader code in its own scope so it gets destroyed when not needed anymore and then you can go on move your files around.

                thamT Offline
                thamT Offline
                tham
                wrote on last edited by
                #12

                @SGaist said in Cannot rename by QFile::rename:

                I'd move the QImageReader code in its own scope so it gets destroyed when not needed anymore and then you can go on move your files around.

                Thanks for the suggestion, I do that already and now it works :)

                No matter it is a bug or not, I think this api is counter intuitive, if QImageReader would lock the file, it should have a close api or mentioned in the document.

                However, I open a bug report at jira.

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  Mishko
                  wrote on last edited by
                  #13

                  I had a something like this problem with file raname after move application from Linux to windows, But I rename file with names which are taked from text file separated by new line. At Linux if have list of names readed from file we must remove only last one symbol ("\n") from string (you cant set file name if at newNamr is cariage move symbol). At windows you must remove TWO symbols from the end of string (new line defined by "\r\n"). May be it will help someone

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    The internals are a bit more complex than that.

                    I'd move the QImageReader code in its own scope so it gets destroyed when not needed anymore and then you can go on move your files around.

                    E Offline
                    E Offline
                    efofex
                    wrote on last edited by
                    #14

                    @SGaist I have just had exactly the same issue with QImageWriter. It keeps the file open/locked on Windows after writing. Moving it to it's own scope fixed the issue. I think that the documentation for QImageWriter needs to be updated to indicate this behaviour.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @efofex Patch submitted.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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