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. [SOLVED] QTemporaryFile - strange behavior

[SOLVED] QTemporaryFile - strange behavior

Scheduled Pinned Locked Moved General and Desktop
qtemporaryfileqdirtemppath
6 Posts 4 Posters 2.5k Views
  • 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.
  • _ Offline
    _ Offline
    _rth_
    wrote on 15 May 2015, 11:28 last edited by _rth_
    #1

    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?

    K 1 Reply Last reply 15 May 2015, 12:41
    0
    • _ _rth_
      15 May 2015, 11:28

      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?

      K Offline
      K Offline
      koahnig
      wrote on 15 May 2015, 12:41 last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • _ Offline
        _ Offline
        _rth_
        wrote on 15 May 2015, 13:01 last edited by
        #3

        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;
        }
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 May 2015, 23:14 last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 16 May 2015, 02:05 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • _ Offline
              _ Offline
              _rth_
              wrote on 18 May 2015, 05:00 last edited by
              #6

              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.

              1 Reply Last reply
              0

              5/6

              16 May 2015, 02:05

              • Login

              • Login or register to search.
              5 out of 6
              • First post
                5/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved