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. QFile::rename problem
Forum Updated to NodeBB v4.3 + New Features

QFile::rename problem

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.5k Views 1 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.
  • S Offline
    S Offline
    SurplusCJ
    wrote on last edited by SurplusCJ
    #1

    Hi, all
    in my project, i wish to rename a file before exit the application.
    here is my problem, i called QFile::rename function to rename the file, the function return true, and i use QFile::exist to check the destination file exist or not, it return true (means the file already exist). but i open the saved path to check the files, it does not exist in th e destination folder.

    Attention: after QFile::rename function returned, i do exit the application (i call the rename function in the main thread, so rename function returned, the application finished)

    here is my rename function:

    bool Utils::renameFile(QString oldName, QString newName)
    {
        if (oldName.isEmpty() == true
                || newName.isEmpty() == true) {
            return false;
        }
        return QFile::rename(oldName, newName);
    }
    
    bool b = Utils::renameFile(mLogFileName, mSavedFileName);
    
        QThread::msleep(50);
    
        QFile file(mSavedFileName);
        int count = 0;
        LOG_D(tlfTAG, "Save file -> file exist : " + QString::number(file.exists()));
        while (file.exists() == false && count < 10) {
            LOG_D(tlfTAG, "Save file -> save file checking...");
            QThread::msleep(20);
            count ++;
        }
    
        if (file.exists() == false) {
            LOG_E(tlfTAG, "Save file -> save failed (file not exist in the folder) : timeout");
            return false;
        }
    

    The ```
    LOG_D(tlfTAG, "Save file -> file exist : " + QString::number(file.exists()));

    and the while loop should never enter.
    
    And if do the rename flow while the application is running (without exit application), the rename flow should be succeed.
    JonBJ jsulmJ 2 Replies Last reply
    0
    • S SurplusCJ

      Hi, all
      in my project, i wish to rename a file before exit the application.
      here is my problem, i called QFile::rename function to rename the file, the function return true, and i use QFile::exist to check the destination file exist or not, it return true (means the file already exist). but i open the saved path to check the files, it does not exist in th e destination folder.

      Attention: after QFile::rename function returned, i do exit the application (i call the rename function in the main thread, so rename function returned, the application finished)

      here is my rename function:

      bool Utils::renameFile(QString oldName, QString newName)
      {
          if (oldName.isEmpty() == true
                  || newName.isEmpty() == true) {
              return false;
          }
          return QFile::rename(oldName, newName);
      }
      
      bool b = Utils::renameFile(mLogFileName, mSavedFileName);
      
          QThread::msleep(50);
      
          QFile file(mSavedFileName);
          int count = 0;
          LOG_D(tlfTAG, "Save file -> file exist : " + QString::number(file.exists()));
          while (file.exists() == false && count < 10) {
              LOG_D(tlfTAG, "Save file -> save file checking...");
              QThread::msleep(20);
              count ++;
          }
      
          if (file.exists() == false) {
              LOG_E(tlfTAG, "Save file -> save failed (file not exist in the folder) : timeout");
              return false;
          }
      

      The ```
      LOG_D(tlfTAG, "Save file -> file exist : " + QString::number(file.exists()));

      and the while loop should never enter.
      
      And if do the rename flow while the application is running (without exit application), the rename flow should be succeed.
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @SurplusCJ
      Your explanation not clear: are you saying "while the app executing the QFile::rename() is still running I do not see/cannot open the renamed file in its new name/location, but after the app exits I do see/can open it there"?

      1 Reply Last reply
      0
      • S SurplusCJ

        Hi, all
        in my project, i wish to rename a file before exit the application.
        here is my problem, i called QFile::rename function to rename the file, the function return true, and i use QFile::exist to check the destination file exist or not, it return true (means the file already exist). but i open the saved path to check the files, it does not exist in th e destination folder.

        Attention: after QFile::rename function returned, i do exit the application (i call the rename function in the main thread, so rename function returned, the application finished)

        here is my rename function:

        bool Utils::renameFile(QString oldName, QString newName)
        {
            if (oldName.isEmpty() == true
                    || newName.isEmpty() == true) {
                return false;
            }
            return QFile::rename(oldName, newName);
        }
        
        bool b = Utils::renameFile(mLogFileName, mSavedFileName);
        
            QThread::msleep(50);
        
            QFile file(mSavedFileName);
            int count = 0;
            LOG_D(tlfTAG, "Save file -> file exist : " + QString::number(file.exists()));
            while (file.exists() == false && count < 10) {
                LOG_D(tlfTAG, "Save file -> save file checking...");
                QThread::msleep(20);
                count ++;
            }
        
            if (file.exists() == false) {
                LOG_E(tlfTAG, "Save file -> save failed (file not exist in the folder) : timeout");
                return false;
            }
        

        The ```
        LOG_D(tlfTAG, "Save file -> file exist : " + QString::number(file.exists()));

        and the while loop should never enter.
        
        And if do the rename flow while the application is running (without exit application), the rename flow should be succeed.
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @SurplusCJ Did you check manually whether the file is there?

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

        S 1 Reply Last reply
        0
        • jsulmJ jsulm

          @SurplusCJ Did you check manually whether the file is there?

          S Offline
          S Offline
          SurplusCJ
          wrote on last edited by
          #4

          @jsulm yes, i do check the file manually in the destination folder.
          and found the expected files not exist.

          jsulmJ JonBJ 2 Replies Last reply
          0
          • S SurplusCJ

            @jsulm yes, i do check the file manually in the destination folder.
            and found the expected files not exist.

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

            @SurplusCJ Are you sure your app is storing the file where you expect it to store it?
            I mean, how did you construct the file path (mSavedFileName)?
            Does mSavedFileName contain a complete/absolute path, or only file name, or only a relative path?
            If it is only file name or relative path then it will be stored relative to the current directory of the process.

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

            1 Reply Last reply
            0
            • S SurplusCJ

              @jsulm yes, i do check the file manually in the destination folder.
              and found the expected files not exist.

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

              @SurplusCJ said in QFile::rename problem:

              @jsulm yes, i do check the file manually in the destination folder.
              and found the expected files not exist.

              Like I asked above, are you saying that is different while the program is still running versus after the program has exited, or does the file never exist?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SurplusCJ
                wrote on last edited by
                #7

                I found the root cause.
                while exit the application, removed the source file in the de-constructor.

                Thanks all of you.

                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