Qt World Summit: Register Today!

QFile::rename problem

  • 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);
        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...");
            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.

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

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

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

    Thanks all of you.

Log in to reply