Important: Please read the Qt Code of Conduct -

QFile copy

  • Using Qt 4.7.1 and WinXP SP3.

    Debugging an issue with QFile::copy, below is the code;
    bool copy(const QString& src, const QString& dst)
    if (QFile::exists(dst))
    // log dst file exists
    return false;
    QFile fileHandler(src);
    if (!fileHandler.copy(dst))
    // log src and dst path
    // log fileHandler.errorString() and fileHandler.error()
    // log QFile::exists(src) and QFile::exists(dst)
    return false;
    return true;

    This problem has occurred once and we have not been able to reproduce it. Here is the sequence of events;

    1. The destination file does not exists prior to copying
    2. The QFile::copy method fails
    3. The src and dst contain a correct file path
    4. The QFile::errorstring returns "Unknown error" and QFile::error returns 0.
    5. After the QFile::copy has failed, both src and dst file exists

    This information is contradictory. The destination file is not present, copying fails but for some reason destination file is now present.

    The errorstring is not very helpful. The QIODevice::errorString() returns "Unknown error" if the internal errorstring variable is empty (QIODevice is the base class of QFile). How can QFile::copy return false with either an empty errorstring or errorstring set to "Unknown error"?

    One option would be that the filename variable inside QFile is empty, however the log proves that is not the case.

  • Moderators

    Might be a bug. But in order to report it on JIRA, you have to be able to reproduce the issue.

    Just a suggestion - check if src exists just like you check the destination. Might prevent problems later on.

  • Check Qt 4.8, there were some internal changes in this area.
    If it works in 4.8 your best option is to upgrade Qt version.

  • That is so strange - contradicting all - QFile::copy() returns false, but file is copied to the destination, which doesn't exist before copy call.

    I understand copy() will return false, if destination is available rather overwriting. This case will not happen in your case, since you check the existence of destination before copy() - very strange.

Log in to reply