[SOLVED] QTemporaryFile - strange behavior



  • Hello.

    I have problem with QTemporaryFile location. In my app, in some point I need to clear the temp file content. I have used the simplest way: remove file and initialize it again. However after this step, QTemporaryFile class changes location, where temp file is created.

    Here is simple app, which simulates the problem:

    #include <QCoreApplication>
    #include <QTemporaryFile>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QTemporaryFile tmpFile;
    
        if (tmpFile.open())
            qDebug() << tmpFile.fileName();
    
        tmpFile.remove();
    
        if (tmpFile.open())
            qDebug() << tmpFile.fileName();
    
        //return a.exec();
        return 0;
    }
    

    And here is the result:

    "C:/Users/theuerom/AppData/Local/Temp/tmpfiletest.Hp3912"
    "C:/Qt5_projects/build-tmpfiletest-Desktop_Qt_5_4_1_MinGW_32bit-Debug/.gq3912"
    

    Is that a bug? Or am I doing something terribly wrong?


  • Moderators

    @rth

    This seems really strange.
    You could check with QDir::tempPath() to check the default temp file location.

    Also you can check in JIRA if there is a known bug.



  • QDir::tempPath() seems to be ok.

    Actually I have solved this problem using file template, which contains QDir::tempPath();

    #include <QCoreApplication>
    #include <QTemporaryFile>
    #include <QDir>
    #include <QDebug>
    
    void printFile(const QString& name)
    {
        QFile file(name);
        if (file.open(QIODevice::ReadOnly)) {
            qDebug() << file.readAll();
            file.close();
        }
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        qDebug() << QDir::tempPath();
    
        QString templ = QString("%1/something_XXXXXX").arg(QDir::tempPath());
    
        QTemporaryFile tmpFile;
        tmpFile.setFileTemplate(templ);
    
        if (tmpFile.open()) {
            qDebug() << tmpFile.fileName();
    
            tmpFile.write("1st attempt");
            tmpFile.flush();
        }
    
        printFile(tmpFile.fileName());
    
        tmpFile.remove();
    
        tmpFile.setFileTemplate(templ);
        if (tmpFile.open()) {
            qDebug() << tmpFile.fileName();
    
            tmpFile.write("2nd attempt");
            tmpFile.flush();
        }
    
        printFile(tmpFile.fileName());
    
        //return a.exec();
        return 0;
    }
    

  • Lifetime Qt Champion

    Hi,

    It still feels like a bug, you really should take a look at the bug report system like @koahnig suggested



  • https://bugreports.qt.io/browse/QTBUG-2557 seems related

    Rather than remove() the file and create a new one you should be able to resize(0) and continue to use the same temporary file.



  • I have quickly gone through existing bugs reported for QTemporaryFile and nothing related found. So I have created new bug: https://bugreports.qt.io/browse/QTBUG-46156. IMO its better to have a few duplicates, that not reported at all :)

    BTW, the resize(0) works fine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.