Important: Please read the Qt Code of Conduct -

QFile / QTextStream

  • Hi everyone,

    I want to do a very simple thing but I can't do it ...
    I have a log file saved in some place (D://test.txt), I use a QTextStream to write text in it.

    if (|QIODevice::Text))
    m_streamFile << msg;

    I create a menu to change its location.
    I just replace the file name:

    m_file.rename(dirName + "/" + fileName + ".txt");

    But when new log messages appear, they are not written in the new file ...

    Any idea ?

    Thanks !

  • before m_streamFile << msg; could you put Q_ASSERT(m_streamFile.device()->isOpen()); ?

  • The API of QFile::rename(...) says:
    "The file is closed before it is renamed."
    So you have to open the file again after renaming...

  • @VRonin said:


    You are right, the file is closed. But if I open it, the previous file content is cleared ...

    if ( | QIODevice::Text))

  • @ClaraCaste Try to open in append mode, otherwise the file will be truncated:
    if ( | QIODevice::Text | QIODevice::Append))

  • It works !
    It seems obvious ...
    Thank you very much !

Log in to reply